Skip to content

Find

search for files in a directory hierarchy.

Command line options

  • -print: 기본값으로 설정되어 있고, 이 옵션이 주어지면 최종 결과를 화면에 출력한다.
  • -o 또는 -or: 앞의 조건 또는 뒤의 조건이 일치해야 한다. (OR)
  • -a 또는 -and: 앞의 조건과 뒤의 조건이 모두 만족해야 한다. (AND)
  • -not: 뒤에 나오는 연산과 일치하지 않는 조건을 찾는다.
  • -type [파일타입]: 파일의 타입을 지정하여 검색한다. 타입 목록은 아래와 같다.
    • b: 블록 특수 파일(block device)
    • c: 캐릭터 특수 파일 (character deice)
    • d: 디렉토리(directory)
    • f: 일반파일(file)
    • l: 심볼릭 링크(link)
    • p: 파이프 (pipe)
    • s: 소켓 (socket)
  • -maxdepth: 파일 경로의 최대 깊이를 지정.
  • -depth: 파일 경로의 깊이.
  • -name filename: 파일 이름으로 찾는다.
  • -atime +n: access time 이 n 이전인 파일을 찾는다.
  • -atime -n: access time이 n 이내인 파일을 찾는다.
  • -amin +n: 마지막 접근시간이 n분 이상 지난 파일.
  • -amin -n: 마지막 접근시간이 n분 이내인 파일 찾기.
  • -mtime +n: n 이전에 변경된 파일을 찾는다.
  • -mtime -n: n 이내에 변경된 파일을 찾는다.
  • -ctime +n: n 이전에 파일의 상태가 변경된 파일을 찾는다.
  • -ctime -n: n 이내에 파일의 상태가 변경된 파일을 찾는다.
  • -perm nnn: 파일 권한이 nnn인 파일을 찾는다.
  • -size n: 사이즈가 n이상인 파일들을 찾는다.
  • -links n: 링크된 개수가 n인 파일들을 찾는다.
  • -user username: user이름으로 찾는다.
  • -group groupname: group 이름으로 찾는다.
  • -exec cmd arg0 arg1 arg2 ... argN \;: 검색된 파일별로 명령을 보낸다. 파일명은 {}을 사용하면 치환되며 \;를 사용하여 명령을 종료한다.
  • -execdir cmd arg0 arg1 arg2 ... argN \;: -exec와 동일. 단, 검색결과시 디렉터리를 제거한 파일명으로 {}을 치환하고 싶을 경우 사용한다.
  • -empty: 파일의 크기가 0인 일반 파일 또는 디렉터리를 찾는다.
  • -anewer [PATH]: [FILEPATH]보다 최근에 접근한 파일 찾기.
  • -newer [FILE]: 가장 최근에 갱신된 모든 파일을 검색한다.

Time format

시간에 대한 입력 서식(Format)은 -atime n[smhdw]와 같다. 여기에서 [smhdw]는 각가 아래와 같은 의미를 갖는다.

  • s: second
  • m: minute (60 seconds)
  • h: hour (60 minutes)
  • d: day (24 hours)
  • w: week (7 days)

예를 들면, -atime -1h30m는, 현 시간부로 1시간 30분 안에 접근(Access)된 파일을 찾는다.

Example

find /path -type f -exec rm '{}' \;
일반 파일을 전부 삭제한다.
find /path -maxdepth 1 -type d
1Depth에 존재하는 모든 디렉토리(폴더)를 찾는다.
find . ! -name "*.png" -and ! -name "*.jpg"
png와 jpg확장자를 갖는 파일을 제외한 모든 파일을 찾는다.
find /etc -type d
디렉토리를 검색.
find /home -executable
실행가능한 파일을 검색
find /home/nestgoer -atime -100 -exec ls -l {} \;
최근 100 시간 이내에 사용된 적이 있는 파일(액세스된 파일) 검색.
분(Minute)단위로 검색하고 싶을 경우 -amin를 사용하면 된다.
find /home -anewer /home/nestgoer/begin -exec ls -l {} \;
지정한 파일보다 더 최근에 사용된 적(액세스된 파일)이 있는 파일 검색
find / -size +100M -exec ls -lh {} \;
파일용량이 큰 파일들을 모두 검색하여 처리
find / -perm 4755 -exec ls -l {} \;
지정한 퍼미션을 가지고 있는 파일 검색
find / -empty -exec ls -l {} \;
용량이 0인 비어있는 파일 검색.
find / -user nestgoer -print
특정 사용자의 소유파일들 검색. (이 경우, nestgoer 사용자를 탐색)
touch -t 200511071100 /tmp/tstmp ; find / -newer /tmp/tstmp -print
특정 시간 이후에 생성된 파일 찾기.
find ./ -type f -size 0 or find ./ -type f -empty
To find all zero size files, simply use
find trains -mindepth 1 -maxdepth 1 -type d -printf '%f\n'
정확히 trains 폴더에 있는 (깊이1) 디렉토리 이름만 출력.
find "$find_dir" -mindepth 1 -maxdepth 1\(-type d -o -type l\)-printf '%f\n'
여러 조건을 사용하고, 그 결과를 다음 명령으로 전달할 경우 \(</code>와 <code>\)를 사용한다.

bash mapfile 입력 방법

Bash:Mapfile 항목 참조. 간단히:

mapfile -t vars < <(find "$find_dir" -mindepth 1 -maxdepth 1 -type d)
echo ${vars[*]}

Troubleshooting

GnuWin32 find Bug?

GnuWin32에서 아래와 같은 현상이 나타날 수 있다.

D:\down>find . -name "*.c"
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

커맨드라인 문법이 정상적이지 않다는 내용인데, 이럴 경우 아래와 같이 진행하면 된다.

D:\down>find . -name "*.[c]"

See also

  • fzf
  • Scooter - 터미널용 인터랙티브 Find & Replace 도구
  • fd - find의 간단하고 빠르며 사용자 친화적인 대체제

Favorite site