다음과 같은 디렉토리 구조가 있습니다.
folder
└── 01
├── test1
└── abc.bz2
└── asd.bz2
├── test2
└── 546.bz2
└── alsj.bz2
├── test3
└── aewr.bz2
└── hlk.bz2
└── test4
└── oiqw.bz2
└── abc.bz2
└── 02
├── test1
├── test2
├── test3
└── test4
└── 03
├── test1
├── test2
├── test3
└── test4
.
.
└── 31
모든 test1..4
디렉토리에는 원격 서버에서 복사 된 많은 수의 bzip 파일이 있습니다. bzip2 -t <filename.bz2>
파일이 손상되었는지 확인 하는 명령 을 알고 있지만 위의 폴더 구조에서 손상된 파일을 모두 확인해야합니다. 그래서 모든 손상된 파일의 목록을 얻기 위해 쉘 스크립트를 작성하는 방법은 무엇입니까? 나는 쉘 스크립트와 Linux를 처음 사용하므로 도움을 주시면 감사하겠습니다.
다음 find
과 함께 사용하십시오 -exec
.
find . -name '*bz2' -exec sh -c 'bzip2 -t "$0" 2>/dev/null || echo "$0 is corrupted"' {} \;
에서 man find
:
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command [...]
따라서 find
위 의 명령은로 끝나는 모든 파일을 찾고 각각에 대해 bz2
작은 sh
스크립트를 실행 합니다. 은 {}
발견 된 각 파일 이름으로 대체됩니다. 이것은 스크립트에 첫 번째 매개 변수 ( $0
)로 전달되어 sh
실행 bzip -t
되고 실패하면 불평합니다. 은 2>/dev/null
다른 오류 메시지를 삭제하여 깨끗하게 유지합니다.
또는 셸을 사용할 수 있습니다. 을 사용하는 경우 하위 디렉터리로 재귀 를 만드는 옵션을 bash
활성화하고 각 bzip 파일을 확인합니다.globstar
**
shopt -s globstar
for file in folder/**/*bz; do bzip2 -t "$file" || echo "$file is corrupted"; done
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다