개요

find 는 파일이나 디렉터리를 찾는 명령어로 이름, 크기, 종류, 생성일등 다양한 조건으로 검색할 수 있으며 검색 결과는 유닉스의 장점인 pipe 를 이용하여 다른 프로그램에서 처리할 수 있습니다.


예로 만든 지 일정 기간이 지난 로그 파일을 찾은 후에 pipe 로 파일 옮기는 명령어인 mv 에 전달하여 특정 폴더로 이동할 수 있습니다.


find 는 다음과 같이 옵션과 찾을 경로, expression 으로 이루어 집니다.

find 사용법

find [options] [path] [expression]
BASH


option

주요 options 은 다음과 같습니다.

option의미비고
-Psymbolic link 일 경우 따라 가지 않음기본 설정
-Lsymbolic link 도 따라감
-Pcommand line argument 를 제외하고는 symbolic link 를 따라가지 않음.


expression

expression 은 Tests, Actions, Operators 가 올수 있으며 생략할 경우 찾은 파일을 목록을 출력하는 기본 액션인 -print 가 적용됩니다.


Tests expression

Tests expression은 특정 조건에 맞는 대상을 선별하기 위한 문법으로 많이 사용하는 구문은 다음과 같습니다.

  • -cnewer orig_file : orig_file 보다 최근에 변경된 파일
  • -ctime n : 파일의 변경일이 n*24 이내인 파일
  • -empty: 빈 파일
  • -gid n : gid 가 n 인 파일
  • -iname pattern-name 과 같지만 대소문자 구분 안 함
  • -iregex pattern: -regex 와 같지만 대소문자 구분 안 함
  • -name pattern:  이름으로 찾음 (*, ?, [] 같은 메타 문자 사용 가능)
  • -perm mode: 권한으로 찾음(Ex: 755)
  • -regex pattern: 정규식으로 찾음
  • -size n[cwbkMG] : 크기로 찾음( +, - 연산자 사용)
    • k: kibi bytes
    • M: megi bytes
    • G: gigi bytes
  • -type c : 파일 유형으로 찾음
    • b block (buffered) special

    • c character (unbuffered) special

    • d: directory
    • f: file
    • l: symbolic link
    • s: socket
  • -uid n: file 의 소유자 id 가 n 인 파일
  • -user name: file 의 소유자 명이 name인 파일


Actions expression

action 은 찾은 파일에 대해서 실행할 액션으로 생략할 경우 -print 가 됩니다.

  • -delete : 파일 삭제
  • -exec command:  지정한 명령어 실행
  • -print : 파일 목록 출력
  • -print0 : 개행 없이 파일 목록 출력


text 로 찾기

특정 문구가 있는 파일 출력

search_directory 이하의 파일중에 파일내 search_string 이 있는 부분 및 해당 파일명 출력합니다.

find search_directory -type f -exec grep search_string /dev/null {} \;
BASH


일치하는 문구는 제외하고 파일명만 출력

grep 의 옵션중에  -l, --files-with-matches 을 주어서 실행

find search_directory -type f -exec grep -l search_string /dev/null {} \;
BASH


여러 패턴 찾기

find directory \( -name "*.py" -o -name "*.html" \)
BASH


검색 결과 처리

xargs 와 파이프로 연결하여 처리


vim 백업 파일 삭제

find directory -name \*~ | xargs rm 
BASH


다른 조건으로 찾기

file size 로 찾기

-size 옵션 뒤에 숫자 크기와 단위(k, M, G 를 명시, (warning) M 과 G는 대문자여야 함)


100k 이상 크기 파일 찾아서 상세 정보 출력

find . -size +100k |xargs ls -l 
BASH



100M 이상 파일 찾아서 상세 정보 출력

find . -size +100M |xargs ls -lh 
BASH

date 로 찾기

https://www.cyberciti.biz/faq/howto-finding-files-by-date/

https://stackoverflow.com/questions/158044/how-to-use-find-to-search-for-files-created-on-a-specific-date

date 관련 옵션

  • -atime : access time 
  • -mtime : modified time
  • -ctime : creation time


주요 사용법

날자가 이후일 경우 +, 이전일 경우 - 뒤에 숫자를 입력

  • -mtime +60: 변경된지 60일 이후
  • -mtime -60: 변경된지 60일 이전
  • -ctime -30: 생성된지 30일 이내


생성된지 일주일 이내인 파일 검색

find -type f -ctime -7
BASH


생성된지 일주일 이후인 파일 삭제

find -type f -ctime +7 | xargs rm -f
BASH

rm 만 사용할 경우 지울 파일이 없으면 "rm: missing operand" 에러가 나므로 -f 옵션을 추가


일주일전에 생성된 파일

find -type f -ctime 7
BASH

접근한지 10일 이후인 파일

파일 access 가 없으면 출력 안 함

find -type f -atime +10
BASH

변경된지 15일 이내인 파일

생성된지 15일 이내인 파일도 포함됨

find -type f -mtime -15
BASH


file type 으로 찾기

directory 만 표시

find /var/www -type d
BASH


file 만 표시

find /var/www -type f
BASH


symbolic link 만 표시

find /var/www -type l
BASH


깨진 symbolic link 파일 찾기

find . -type l ! -exec test -e {} \; -print
BASH


inode number 로 찾기

-inum 옵션을 사용하면 해당 inode number 를 가진 파일을 찾을 수 있으며 하드 링크 파일을 찾을 때 유용합니다.

find . -inum 12345 | xargs ls -l
BASH


pipe 로 검색 결과 처리

찾은 파일 처리

find 로 찾은 파일에 대해 처리가 필요할 경우 xargs 명령어를 pipe 로 연결해서 처리

xargs 로 연결해서 동영상 정보 추출

find . -name *.mp4  | xargs  ffprobe -v quiet -print_format json -show_format -show_streams
BASH


파일/폴더에 공백이 있을 경우 delimiter 지정

xargs 는 공백을 구분자로 처리해서 파일명에 공백이 있을 경우 오작동하므로 -d 옵션으로 개행 문자를 delimiter 로 지정

find . -name tour.xml | xargs -d '\n'  ffprobe -v quiet -print_format json -show_format -show_streams
BASH


같이 보기

Ref