본문 바로가기
OS 운영체제/LINUX

find (파일 검색 명령어)

by yororing 2024. 4. 5.

01 find 명령어란

1. find 정의

  • '찾다'

2. find 명령어 기능

  • Linux 에서 파일 및 디렉토리를 검색할 때 사용
  • 옵션을 사용해 원하는 파일 및 디렉토리 검색 가능

3. find 명령어 문법

# find [path] [options] [expression]
  • path (경로): Starting directory for the search
  • options (옵션): Additional settings or conditions for the search
  • expression (표현식): Criteria for filtering and locating files
  • find 명령에는 몇 가지 옵션(option)과 많은 수의 표현식(expression)이 존재
  • find 명령의 검색 결과가 지정된 expression의 조합에 따라 결정되기 때문에 일반적인 Linux 명령어와는 조금 달리 find 명령은 option보다 expression(표현식)이 더 많이 사용됨

02 find 명령어 옵션/표현식

1. 옵션 (options ) 종류

옵션 설명
-H 심볼릭 링크를 따라가지 않으나 Command Line Argument를 처리할 땐 예외
-L 심볼릭 링크에 연결된 파일 정보 사용
-P 심볼릭 링크를 따라가지 않고 심볼릭 링크 자체 정보 사용
-D all|help|tree|search|stat|rates|opt|exec 디버그 (진단) 정보 출력
원하는 결과가 안 나올 때 원인 알아보기 위해 사용
디버그 옵션 목록은 쉼표(,)로 구분됨
    - all: 다른 모든 디버그 옵션 활성화 (help 제외)
     - help: 디버그 옵션들 설명 출력
     - tree: 표현식 나무 (expression tree)를 원래의 형태와 최적화된 형태로 출력
     - search: navigate the directory tree verbosely
     - stat: print messages as files are examined with the stat and lstat system calls. The find program tries to minimize such calls
     - rates: prints a summary indicating how often each predicate succeeded or failed
     - opt: 표현식 최적화 나무 (optimization tree, -O option 참조) 관련 진단 정보 출력
     - exec: -exec, -execdir, -ok, -okdir 관련 진단 정보 출력 
-Olevel 쿼리 최적화 (optimization) 활성화
find 프로그램은 실행 속도를 높이기 위해 전체적인 효과를 유지하여 test expression 들을 재배열 함
즉, 부작용을 가진 predicate들은 서로에 대해 재배열되지 않음
level 별 수행되는 최적화(optimization):
     0: 최적화 level 1과 동일
     1 (기본값): 파일/디렉토리 이름과 관련된 test expression (예: -name, -regex)들이 우선적으로 수행되도록 표현식이 재배열됨
     2: Any -type or -xtype tests are performed after any tests based only on the names of files, but before any tests that require information from the inode. On many modern versions of Unix, file types are returned by readdir() and so these predicates are faster to evaluate than predicates which need to stat the file first.  If you use the -fstype FOO predicate and specify a filesystem type FOO which is not known (that is, present in `/etc/mtab') at the time find starts, that predicate is equivalent to -false.
     3: the full cost-based query optimiser is enabled. The order of tests is modified so that cheap (i.e. fast) tests are performed first and more expensive ones are performed later, if necessary.  Within each cost band, predicates are evaluated earlier or later according to whether they are likely to succeed or not.  For -o, predicates which are likely to succeed are evaluated earlier, and for -a, predicates which are likely to fail are evaluated earlier.

 

2. 표현식 (expression) 종류

  • 표현식은 매칭된 파일들을 어떻게 찾고 해당 파일들에 대해 무엇을 수행할지를 설명하는 쿼리 명세 같은 것
  • 표현식의 분류:
표현식 분류 설명 예시 예시 설명/기능
Tests True/False를 반환, 보통 파일/디렉토리의 어떤 속성에 관해 결정됨 -empty <파일> <파일>이 비어있을 경우 True 반환
Actions True/False를 반환, 부작용 (예: standard output에 어떤 내용을 출력하는 것 등)이 있음 -print standard output에 현재 파일의 이름을 출력 
Global Options 항상 True 반환, 명령줄의 어느 부분에서든 지정된 test 및 action의 작동에 영향을 끼침 -depth 옵션 find가 depth-first 순서로 파일 시스템을 탐색하도록 
Positional Options 항상 True 반환, 오로지 뒤에 오는 test 또는 action에만 영향을 끼침 (Global Options과 상이) -regextype 옵션 정규 표현식 언어를 명령줄에서 나중에 발생하는 정규 표현식에 지정하여 영향을 끼침 
Operators 표현식 내의 항목들을 결합함, 연산자 없는 경우 -a(기본값) -o, -a -o: 논리 OR, -a: 논리 AND
  • -print action은 표현식 조건에 맞는 모든 파일에 관하여 실행됨  (-prune 이나 -quit이 있지 않은 이상)
  • 기본값 -print 를 inhibit(억제?)하는 action들: -delete-exec, -execdir-ok, -okdir-fls-fprint-fprintf-ls-print-printf
  • -delete action은 옵션과 같이 작동함 (-depth를 imply하기에)

1) Tests

  •  

2) Actions

  •  

3) Global Options

  •  

4) Positional Options

  • 항상 True를 반환, 오로지 뒤에 오는 test 또는 action에만 영향을 끼침 (Global Options과 상이)
종류 설명
-daystart Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and -mtime) from the beginning of today rather than from 24 hours ago. This option only affects tests which appear later on the command line.
-follow

*비고: Deprecated; use the -L option instead.
Dereference symbolic links.  Implies -noleaf.  The -follow option affects only those tests which appear after it on the command line.  Unless the -H or -L option has been specified, the position of the -follow option changes the behaviour of the -newer predicate; any files listed as the argument of -newer will be dereferenced if they are symbolic links.  The same consideration applies to -newerXY, -anewer and -cnewer.  Similarly, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself.  Using -follow causes the -lname and -ilname predicates always to return false.
-regextype type Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. To see which regular expression types are known, use -regextype help.  The Texinfo documentation (see SEE ALSO) explains the meaning of and differences between the various types of regular expression.
-warn, -nowarn Turn warning messages on or off.  These warnings apply only to the command line usage, not to any conditions that find might encounter when it searches directories.  The default behaviour corresponds to -warn if standard input is a tty, and to -nowarn otherwise.  If a warning message relating to command-line usage is produced, the exit status of find is not affected.  If the POSIXLY_CORRECT environment variable is set, and -warn is also used, it is not specified which, if any, warnings will be active.

5) Operators 

  •  

참조

  1. https://recipes4dev.tistory.com/156
  2. https://man7.org/linux/man-pages/man1/find.1.html
  3. https://www.redhat.com/sysadmin/linux-find-command
  4.