find
명령어는 리눅스/유닉스 계열 운영체제에서 파일 및 디렉토리를 검색하는데 사용되는 강력한 도구이다. 이 명령어는 다양한 옵션을 통해 다양한 케이스에서 유연한 검색을 가능하게 해준다. 이 글에서는 기본 사용법과 자주 사용되는 예제들을 알아본다.
find
기본 구문
find [path] [expression] [action]
[path]
: 검색을 할 디렉토리 경로를 지정한다.[expression]
: 다양한 옵션으로 검색 조건을 지정한다.[action]
: 찾은 결과에 대해 어떤 동작을 수행할 것인지 지정한다.
Examples
파일 이름으로 검색
filename.txt
이름을 갖는 파일을 /tmp
디렉토리에서 찾는다.
$ find /tmp -name "filename.txt"
/tmp/filename.txt
특정 확장자를 가진 파일 찾기
/var/log
디렉토리에서 log
확장자를 갖는 파일을 찾는다.
$ find /var/log -type f -name *.log
/var/log/ubuntu-advantage-timer.log
/var/log/kern.log
/var/log/dpkg.log
/var/log/fontconfig.log
... ...
파일 크기 기준 검색
/usr/lib
디렉토리에서 파일 크기가 1M 이상인 파일을 찾는다.
$ find /usr/lib -type f -size +1M
/usr/lib/file/magic.mgc
/usr/lib/wsl/lib/libd3d12core.so
/usr/lib/wsl/drivers/adp80xx.inf_amd64_efb36fdc260e8bc8/adp80xx.sys
/usr/lib/wsl/drivers/amdfdans.inf_amd64_4642113bda5f5b9b/anronnxlib.dll
... ...
특정 사용자 소유의 파일 찾기
/var/log
디렉토리에서 syslog
이 사용자인 파일을 찾는다.
$ find /var/log -type f -user syslog
/var/log/kern.log
/var/log/kern.log.3.gz
/var/log/auth.log.2.gz
/var/log/auth.log.1
... ...
특정 그룹에 속한 파일 찾기
$ find /etc -type f -group shadow
/etc/shadow
/etc/gshadow-
/etc/gshadow
/etc/shadow-
... ...
/etc
디렉토리에서 그룹이 shadow
인 파일을 찾는다.
특정 시간 이후에 변경된 파일 찾기
/var/log
디렉토리에서 2일 내에 변경된 파일을 찾는다.
$ find /var/log -type f -mtime -2
/var/log/lastlog
/var/log/kern.log
/var/log/dpkg.log
/var/log/auth.log.1
... ...
빈 파일 및 디렉토리 찾기
현재 디렉토리에서 크기가 0인 빈 파일을 찾는다.
$ find . -type f -empty
./empty.txt
파일 타입에 따라 검색
현재 폴더에서 디렉토리 타입을 찾느다.
$ find . -type d
.
./md5
파일 내용으로 검색
현재 디렉토리에서 특정 문자열이 포함된 파일을 찾는다.
find . -type f -exec grep -l "pattern" {} \;
찾은 파일 삭제
/tmp
디렉토리에서 특정 조건에 맞는 파일, 즉 확장자가 tmp인 파일을 찾아 삭제한다.
find /tmp -type f -name "*.tmp" -delete
파일 퍼미션으로 검색
/etc
디렉토리 에서 퍼미션이 644
인 파일을 찾는다.
find /etc -type f -perm 644
파일 찾은 후 명령어 실행
/tmp
디렉토리에서 파일 이름이 “pattern”인 파일을 찾은 후 해당 파일에 대해 chmod 777
명령어를 실행한다.
find /tmp -type f -name "pattern" -exec chmod 755 {} \;
파일 중첩 검색
특정 디렉토리 아래 모든 파일을 검색한다.
find /path/to/search -type f
파일 찾기 및 출력 제한
현재 디렉토리에서 확장자가 txt
인 파일을 찾아 제일 처음 찾은 하나만 출력한다.
find . -type f -name "*.txt" | head -n 1
특정 경로 제외하고 검색
특정 경로를 제외하고 검색한다.
find /path/to/search -type f -not -path "/path/to/exclude/*"
생성 및 수정된 시간으로 검색
생성된 시간이나 수정(Update)된 시간을 특정해서 검색하는 방법은 아래 글을 참고한다.
참고 사이트
- find(1) – Linux man page:
find
명령어에 대한 공식 문서.