find – 폴더에서 특정조건을 가진 파일 찾기
find 는 폴더에서 특정 조건에 맞는 파일을 찾을때 사용하는 리눅스 명령어입니다.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[root@server ~]# find --help Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression] default path is the current directory; default expression is -print expression may consist of: operators, options, tests, and actions: operators (decreasing precedence; -and is implicit where no others are given): ( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2 EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2 positional options (always true): -daystart -follow -regextype normal options (always true, specified before other expressions): -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xdev -ignore_readdir_race -noignore_readdir_race tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE -nouser -nogroup -path PATTERN -perm [+-]MODE -regex PATTERN -readable -writable -executable -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N -used N -user NAME -xtype [bcdpfls] -context CONTEXT actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ; -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ; |
응용으로는…
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 현재 폴더내에 180일이 지난 파일을 찾아서 내용 삭제 [root@server ~]# find ./ -type f ! \( -mtime -180 \) -exec cp -f /dev/null {} \; # 현재 폴더내에 한 단계 아래까지만 체크하여 180일이 지나도 특정 네임을 가진 파일 제외하고 파일을 찾기 [root@server ~]# find ./ -type f -maxdepth 1 ! \( -mtime -180 -or -name weblog -or -name .bash_* -or -name .emacs -or -name myinfo\); # 현재 폴더내에 한 단계 아래까지만 체크하여 31일이 지난 파일의 내용을 삭제 [root@server ~]# find ./ -type f -maxdepth 1 ! \( -mtime -31 \) -exec cp -f /dev/null {} \; # 현재 폴더이하에서 .html 이 붙은 모든 파일을 찾아서 .asp를 .html로 내용 수정(일괄 치환) [root@server ~]# find . -type f -name '*.html' -exec perl -pi -e 's/.asp/.html/g' {} \; # 현재 폴더이하에서 모든 파일을 열어 ro521.com 이 있는 라인을 찾아서 삭제 (침해파일 일괄 수정할때 사용) [root@server ~]# find . -type f -exec vim -c ":g/ro521.com/d" -c ":x" "{}" \; # 현재 폴더이하에서 모든 파일을 열어 ro521.com 이 있는 파일 찾기 [root@server ~]# find . -type f | xargs grep 'ro521.com' | sed 's/:/ /g' | awk '{print $1}' # 현재 폴더이하에서 특정유저의 파일을 찾아 그 파일의 소유권 교체 [root@server ~]# find . -type f -user '아이디' -exec chown 아이디.그룹 {} \; # 현재 폴더이하에서 .html 이 붙은 파일을 찾아서 압축하기 [root@server ~]# tar cfzp - `find . -name *.html ` > test.tar.gz # /home 폴더이하에서 .php 와 .htm 이 붙은 파일을 찾아 그 파일의 퍼미션을 644로 변경 [root@server ~]# find /home* -type f \( -name '*.php' -or -name '*.htm*' \) -exec chmod 644 {} \; |