OS/Linux

[리눅스] find 사용 예

1125521101 2017. 9. 13. 09:38

---------------------------------------------------------------------------------------------------

find . -name '파일명검색어' -exec sh -c "명령어1; 명령어2; 명령어3;" \;


[root@zetawiki test]# ll

total 20

-rw-r--r-- 1 root root 7875 Jun 15 20:45 1.txt

-rw-r--r-- 1 root root 9450 Jun 15 20:45 2.txt

[root@zetawiki test]# find . -name '*.txt' -exec sh -c "tar cjvf {}.bz2 {}; rm -f {};" \;

./2.txt

./1.txt

[root@zetawiki test]# ll

total 8

-rw-r--r-- 1 root root 1189 Jun 15 20:51 1.txt.bz2

-rw-r--r-- 1 root root 1193 Jun 15 20:51 2.txt.bz2



---------------------------------------------------------------------------------------------------

특정한 파일을 찾아서 특정 디렉토리에 이동

find ./ -name "*.jpg" -type f -exec cp {} /movedir \;



일정기간 30일이 경과된 파일을 찾아서 삭제

find ./ -ctime +30 -type f -exec rm -f {} \;



특정한 파일을 찾아서 모두 압축

find ./ -name "*.jpg" -type f | xargs tar cjvf virus.gz 



2001년1월1일부터 2007년 12월31까지 파일을 찾기

touch -t 200101010000 1

touch -t 200712312359 2

find / -newer 1 ! -newer 2 -ls



특정디렉토리(하위검색은 제외하면서)의 20일지난 폴더 삭제

/usr/bin/find /backup/data/ -maxdepth 1 -type d -mtime +20 -exec rm -Rf {} \;




출처 https://zetawiki.com/wiki/Find_exec_%EC%97%AC%EB%9F%AC_%EB%AA%85%EB%A0%B9%EC%96%B4_%EC%8B%A4%ED%96%89

출처 http://blog.bbom.org/44