리눅스 find 명령어, 옵션 사용법


리눅스 find 명령어 사용법과 find 명령어 옵션에 대해서 예제를 사용하여 설명드립니다. find 명령어는 파일을 검색 할 뿐만 아니라, 파일 크기, 소유자 권한등을 이용하여 검색할 수 도 있습니다. 그리고 exec 등의 옵션과 다른 명령어를 함께 사용하면 소스코드 분석에도 사용됩니다.

리눅스 find 명령어

리눅스 find 명령어 사용법

아래는 find 명령어의 사용방법으로 상당히 많은 옵션들이 지원되는 것을 알 수 있습니다. 가장 많이 사용하는 find 명령어 형태는 [path…] [expression] 형태입니다.

Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]

[expression] 형태는 operator, option, action 등이 지정 될 수 있는데, 아래의 명령어를 실행하면, 보다 상세한 내용을 확인 할 수 있습니다.

$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [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 ;

Valid arguments for -D:
exec, help, opt, rates, search, stat, time, tree
Use '-D help' for a description of the options, or see find(1)

Please see also the documentation at http://www.gnu.org/software/findutils/.
You can report (and track progress on fixing) bugs in the "find"
program via the GNU findutils bug-reporting page at
https://savannah.gnu.org/bugs/?group=findutils or, if
you have no web access, by sending email to <bug-findutils@gnu.org>.

find 명령어 예제

아래의 내용은 find 명령어를 실행하는 기본 예제 입니다. find 명령어 다음 검색 하고자 하는 디렉터리를 지정하면 됩니다.

$ find
$ find .
$ find . -ls
리눅스 find 명령어

아무런 옵션없이 실행하게 되면 디렉터리와 파일의 목록을 출력하게 됩니다. find 명령어 다음 경로를 지정하면, 해당 경로의 파일과 디렉터리 내용을 출력하게 됩니다.

파일 검색

find 명령어를 이용하여 파일을 검색해 보도록 하겠습니다.

파일명을 검색 할 때에는 -name 옵션을 추가하여 검색하면 됩니다. 아래의 명령어는 /etc 위치에서 host.conf 파일을 검색하는 명령어 입니다.

$ sudo find /etc -name "host.conf"

파일 이름을 정확하게 모를 경우, 와일드 문자(‘*’)를 사용하여 검색하면 됩니다. 다음 명령어는 host으로 시작하는 파일을 검색하는 명령어 입니다.

$ sudo find /etc -name "host*"

파일 크기 검색

파일의 크기를 이용하여 검색을 할 수 있는데, 만약 300MB 이상의 파일을 찾기 위해서는 아래의 명령어를 사용하면 됩니다. 특정한 크기보다 작은 파일을 검색하기 위해서는 -기호를 대신 사용 하면 됩니다.

$ sudo find /usr -size +300M

find 명령어 옵션

exec 명령어 실행

find 명령어와 함께 동시에 다른 명령어를 실행 할 수 있습니다. 파일을 삭제, 이동 등 파일을 관리하거나 소스 코드등을 분석 할때 유용하게 사용될 수 있습니다. 아래의 명령어는 find 명령어와 grep 명령어를 함께 사용한 예 입니다.

현재 디렉터리에 있는 모든 파일을 grep 명령어로 검사하면서 “DB_NAME” 이라는 문자열이 있을 경우 파일이름, 라인 번호와 함께 내용을 출력하게 됩니다.

$ find ./ -exec grep -Hn "DB_NAME" {} \; 2>/dev/null

find 명령어를 실행하는데 권한 등의 문제가 있을 경우 발생하는 에러 메세지는 리다이렉트 시키는 명령어가 포함되어 있습니다.

find 명령어와 함께 grep 명령어를 사용하면 사용하면 처음 접하는 소스코드 등을 빠르고 쉽게 분석이 가능합니다.

추가적으로 소스코드 분석을 위해서 vim 편집기 사용 방법에 대해서는 아래의 글을 참고해 주시기 바랍니다.

vi 편집기 사용법

( 본문 인용시 출처를 밝혀 주시면 감사하겠습니다.)