15 일보다 오래된 SQL 파일을 삭제하는 명령을 해결하려고합니다.
찾기 부분이 작동하지만 rm이 아닙니다.
rm -f | find -L /usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups -type f \( -name '*.sql' \) -mtime +15
삭제하려는 파일의 목록을 쫓아 내지 만 삭제하지는 않습니다. 경로가 정확합니다.
usage: rm [-f | -i] [-dIPRrvW] file ...
unlink file
/usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/20120601.backup.sql
...
/usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/20120610.backup.sql
내가 뭘 잘못하고 있죠?
답변
당신은 실제로 배관된다 rm
의 출력 의 입력에 find
. 당신이 원하는 것은의 출력을 사용하는 find
등의 인수 로를 rm
:
find -type f -name '*.sql' -mtime +15 | xargs rm
xargs
표준 입력을 다른 프로그램의 인수로 “변환”하거나 man
페이지 에보다 정확하게 입력 할 수있는 명령입니다 .
표준 입력에서 명령 행 작성 및 실행
파일 이름에 공백 문자가 포함될 수 있으면 다음을 수정해야합니다.
find -type f -name '*.sql' -mtime +15 -print0 | xargs -0 rm
그러나 실제로 find
이것에 대한 지름길이 있습니다 : -delete
옵션 :
find -type f -name '*.sql' -mtime +15 -delete
다음과 같은 경고에 유의하십시오 man find
.
Warnings: Don't forget that the find command line is evaluated
as an expression, so putting -delete first will make find try to
delete everything below the starting points you specified. When
testing a find command line that you later intend to use with
-delete, you should explicitly specify -depth in order to avoid
later surprises. Because -delete implies -depth, you cannot
usefully use -prune and -delete together.
PS 표준 입력에서 파일 이름을 기대하지 않기 rm
때문에 직접 파이핑 하는 것은 옵션 rm
이 아닙니다. 현재하고있는 일은 뒤로 파이핑하는 것입니다.
답변
find /usr/www/bar/htdocs -mtime +15 -exec rm {} \;
/usr/www/bar/htdocs
15 일이 지난 파일을 선택 하여 제거합니다.
답변
또 다른 간단한 방법은 locate
명령 을 사용하는 것 입니다. 그런 다음 결과를로 파이프하십시오 xargs
.
예를 들어
locate file | xargs rm
답변
* .sql 백업 파일이있는 디렉토리에 있지 않다고 가정합니다.
find /usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/*.sql -mtime +15 -exec rm -v {} \;
위의 -v 옵션은 삭제 될 때 삭제중인 파일을 자세하게 출력합니다.
먼저 삭제 될 파일을 나열하고 싶습니다. 예 :
find /usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/*.sql -mtime +15 -exec ls -lrth {} \;
답변
