루프 내에서 find 명령의 출력을 파일에 씁니다.

루프 내에서 find 명령의 출력을 파일에 씁니다.

루프에 중첩된 명령이 있고 find출력을 파일에 쓰고 실시간으로 볼 수 있기를 원합니다. 그러나 지금까지 시도한 모든 방법은 이를 달성하지 못했습니다.

while명령은 다음과 같습니다

while read -r LINE; do find "$LINE" -name "*Bop*" ; done < /drives/d/dirs_to_search.txt

위의 명령을 실행하면,일치하는 하위 디렉터리 목록을 볼 수 있습니다.내 터미널 창에 인쇄됩니다. 나는 원해요이 일치 목록으로 계속, 하지만동시에 파일에 쓰기.

지금까지 내가 시도한 결과는 다음 find과 같은 파일 에 기록되었습니다 matched_subdirs.

while read -r LINE; do find "$LINE" -name "*Bop*" | tee /drives/d/matched_subdirs.txt ; done < /drives/d/dirs_to_search.txt

while read -r LINE; do find "$LINE" -name "*Bop*" ; done < /drives/d/dirs_to_search.txt | tee /drives/d/matched_subdirs.txt

while read -r LINE; do find "$LINE" -name "*Bop*" -print; done < /drives/d/dirs_to_search.txt | tee /drives/d/matched_subdirs.txt

while read -r LINE; do find "$LINE" -name "*Bop*" > /drives/d/matched_subdirs.txt ; done < /drives/d/dirs_to_search.txt

while read -r LINE; do find "$LINE" -name "*Bop*"; done < /drives/d/dirs_to_search.txt > /drives/d/matched_subdirs.txt

답변1

당신이 보여준 명령~해야 한다,테든이 말했듯이, 일하다.

제거하는 또 다른 방법 tee:

rm -f /drives/d/matched_subdirs.txt
while IFS= read -r pathname; do
    find "$pathname" -name '*Bop*' -print \
        -exec sh -c 'printf "%s\n" "$@" >>matched' sh {} +
done <dirlist

그러면 find검토할 수 있도록 찾은 경로 이름이 인쇄된 다음 포함된 쉘 스크립트를 사용하여 결과 파일에 기록됩니다.

출력 파일에서 경로 이름을 어떻게 처리할 계획인지 조금 걱정됩니다. 나중에 루프에 사용할 계획이라면 find내부에서 직접 수행하는 것이 좋습니다. 그렇지 않으면 줄 바꿈이 포함된 이상한 파일 이름으로 인해 문제가 발생할 수 있습니다.


당신이 말한 댓글에서루프에서 발견된 경로 이름을 사용하여 실행하려고 합니다 rsync. 각 경로 이름을 호출하면 rsync다음과 같습니다.매우천천히, 직접 수행하는 것이 좋습니다 rsync.

while IFS= read -r pathname; do
    rsync -avR --include='*/' --include='*Bop*' --exclude='*' --prune-empty-dirs "$pathname" target
done <dirlist

이것은 dirlist귀하의 디렉터리가 포함된 파일입니다.

예:

$ tree
.
|-- dirlist
`-- source
    |-- a
    |   |-- dir-1
    |   |   |-- somefile_Bop_here
    |   |   `-- someotherfile
    |   |-- dir-2
    |   |   |-- somefile_Bop_here
    |   |   `-- someotherfile
    |   `-- dir-3
    |       |-- somefile_Bop_here
    |       `-- someotherfile
    |-- b
    |   |-- dir-1
    |   |   |-- somefile_Bop_here
    |   |   `-- someotherfile
    |   |-- dir-2
    |   |   |-- somefile_Bop_here
    |   |   `-- someotherfile
    |   `-- dir-3
    |       |-- somefile_Bop_here
    |       `-- someotherfile
    `-- c
        |-- dir-1
        |   |-- somefile_Bop_here
        |   `-- someotherfile
        |-- dir-2
        |   |-- somefile_Bop_here
        |   `-- someotherfile
        `-- dir-3
            |-- somefile_Bop_here
            `-- someotherfile

13 directories, 19 files
$ cat dirlist
source/a
source/b/dir-2

(여기서 루프를 실행하세요)

$ tree target
target
`-- source
    |-- a
    |   |-- dir-1
    |   |   `-- somefile_Bop_here
    |   |-- dir-2
    |   |   `-- somefile_Bop_here
    |   `-- dir-3
    |       `-- somefile_Bop_here
    `-- b
        `-- dir-2
            `-- somefile_Bop_here

7 directories, 4 files

나는 -R( --relative)를 사용하기로 결정했다. 그것 없이는 나는 얻을 것이다

target
|-- a
|   |-- dir-1
|   |   `-- somefile_Bop_here
|   |-- dir-2
|   |   `-- somefile_Bop_here
|   `-- dir-3
|       `-- somefile_Bop_here
`-- dir-2
    `-- somefile_Bop_here

관련 정보