linux + "파일" 경고가 있는 파일 찾기 + 삭제삭제됨"

linux + "파일" 경고가 있는 파일 찾기 + 삭제삭제됨"

find파일 이름의 일부를 사용하여 파일을 삭제할 수 있습니다

find -type f -name '*file_name*' -delete

파일 삭제 메시지를 전체 파일 이름과 함께 인쇄하려면 위 구문에 무엇을 추가해야 합니까?

답변1

-print옵션 만 추가하세요

find -type f -name '*file_name*' -print -delete

또는 삭제 전 확인이 필요한 경우 사용 -ok(파일별 확인)

find -type f -name '*file_name*' -ok rm -- {} \;

답변2

find여러 가지 "조치"가 취해질 수 있으며 실제로 이는 모두 조건으로도 사용됩니다. where가 나타내는 것과 -name "*something*" -delete -print동일-name "*something*" -a -delete -a -print-a그리고, C의 연산자와 동일한 단락 논리로 작동하므로 &&후자의 명령/작업/테스트는 이전 명령/작업/테스트가 성공한 경우에만 실행됩니다.

따라서 -delete -print실제 삭제된 파일의 이름이 인쇄됩니다.

$ mkdir test test/ro ; touch test/foo.txt test/ro/bar.txt; chmod 555 test/ro
$ find test -name "*.txt" -delete -print 
find: cannot delete ‘test/ro/bar.txt’: Permission denied
test/foo.txt

를 사용하면 -print -delete먼저 인쇄가 되고, find삭제할 수 없는 파일 이름도 출력됩니다.

또는 더 자세한 출력을 원하는 경우(GNU 찾기):

$ find test -name "*.txt" -delete -printf 'deleted: %p\n'

삭제된 파일과 삭제되지 않은 파일에 대해 서로 다른 메시지를 받아 이를 수행할 수도 있습니다(물론 일반적인 오류 메시지로 인해 이 작업이 약간 중복됩니다).

$ find test -name "*.txt" \( -delete -printf 'deleted: %p\n' -o 
                             -printf "could not delete: %p\n" \)

답변3

삭제할 때 전체 파일 이름을 표시하고 삭제하기 전에 전체 파일 이름을 표시하려는 경우

find . -type f -iname "*file_name*"  -print -exec rm -rvf {} \;

관련 정보