Linux 파일 찾기 검색 find 사용 방법

리눅스에서 파일이 있는지 또는 어디에 있는지 찾고 싶은 경우가 있습니다.

파일을 찾고 싶은 경우에 리눅스에서 사용하는 명렁어는 find입니다.

find를 사용해 파일을 검색하는 방법을 알아보겠습니다.

 

find

find 명령어의 기본 사용 방법을 알아보겠습니다.

find 파일이름 또는 폴더이름

 

find 명령어에 지정한 파일을 검색하거나 폴더를 지정하면 폴더에 있는 파일을 표시합니다.

 

[root@localhost test]# ll
total 20
drwxr-xr-x. 3 test ftp  17 Feb 28 19:15 data
-rw-r--r--  1 root root  2 Jun 18 09:41 log.txt
-rw-r--r--  1 root root  2 Jun 18 09:41 session.log
-rw-r--r--  1 root root  2 Jun 18 09:41 test1.txt
-rw-r--r--  1 root root  2 Jun 18 09:41 test.txt
-rw-r--r--  1 root root  2 Jun 18 09:41 web.log
[root@localhost test]# find data
data
data/TEST
[root@localhost test]# find log.txt
log.txt
[root@localhost test]#

 

find에는 여러 옵션을 지정할 수 있습니다.

정규식을 사용하지 않아도 파일 검색이 가능하지만, 옵션과 함께 정규식을 사용하면 더욱더 강력한 검색 기능을 사용할 수 있습니다.

find에서 사용하는 정규식은 간단한 정규식만 사용하기 때문에 정규식을 몰라도 크게 상관은 없습니다.

find커맨드를 사용 예제를 보면서 여러 패턴을 알아보겠습니다.

 

find 표현식(expression)

find커맨드와 같이 사용할 수 있는 표현식입니다.

표현식이라고 말하지만 옵션이라고도 생각하면 됩니다.

표현식의미
-name현재 디렉터리에서 .txt로 끝나는 모든 파일 및 디렉터리를 검색
$ find -name ‘*.txt’
-user지정한 사용자의 파일이나 디렉터리를 검색
-group지정한 그룹 소유의 파일이나 디렉터리를 검색
-uid지정한 UID소유의 파일이나 디렉터리를 검색(아이디번호로 찾기)
-gid지정한 UID 소유의 파일이나 디렉터리를 검색(그룹번호로 찾기)
-perm지정한 권한으로 설정 되어 있는 파일이나 디렉터리를 검색(permission의 약자)

ex) -perm 755 : 권한이 755로 설정된 파일이나 디렉터리
-type지정한 파일 유형을 검색

d: 디렉터리
f: 일반 파일
l: 링크 파일
b: 블록 디바이스
c: 캐릭터 디바이스
p:파이프 디바이스
s:소켓 파일


(주로 -type f 옵션이나 -type d 옵션을 많이 쓴다)
-atime naccess time 접근 시간 기준으로 검색.
n   : n일 이전에 접근한 파일 검색(exactly)
+n : n보다 오래된 것을 기준 (greater),
-n  : n일부터 지금까지 즉 n일 내에 접근했던 파일을 검색. (less)
-ctime nchange time 수정 시간 기준으로 검색
-mtime nmodify time 변경 시간 기준으로 검색
-mmin n mmin은 분이 기준 (mtime은 기준이 day)
-empty빈 파일(파일의 크기가 0일 경우)이나 빈 디렉터리를 검색
-execfind로 파일을 검색후 리눅스 명령어를 바로 실행 가능
-ls‘ls -l’과 같음
-size nn블록 길이의 파일을 검색.

+n은 n블록보다 큰 파일을 검색(greater)
-n은 n블록보다 작은 파일을 검색 (less)

보통 nc라는 형태로 많이 사용되는데
n은 문자 길이의 파일을 의미하고, c는 character를 의미하여 1문자는 1바이트이므로 보통 바이트 단위로 인식해서 사용한다.
-inum nnode번호가 n인 파일 검색
-iname대소문자 구별 안하고 이름으로 검색 (insensitive의 약자)
-maxdepth n검색할때 지정한 n만큼 서브 디렉터리를 들어가 검색.
-mindepth nmaxdepth의 반대.
-prune찾은 결과 중 디렉터리가 해당될 때 그 디렉터리 이름만 출력 (하위 디렉터리에 해당되는게 있어도 들어가서 검색하지 않는다)
-newer옵션 뒤에 적힌 파일보다 최근에 변경된 파일을 검색

ex) find -newer ‘abc.c’
abc파일보다 최근에 변경된 파일을 검색
-cnewer앞의 newer가 내용 변경이라면 cnewer라면 파일 상태
2>/dev/null커맨드 마지막에 옵션을 붙이면 접근 권한이 없는 파일과 폴더는 화면에 출력하지 않음

 

 

find 명령어 샘플

find 커맨드와 표현식(옵션)을 사용해 검색하는 샘플을 보겠습니다.

사용 예제find 커맨드
현재 디렉토리에 있는 파일 및 디렉토리 리스트 표시find
대상 디렉토리에 있는 파일 및 디렉토리 리스트 표시find [PATH]
현재 디렉토리 아래 모든 파일 및 하위 디렉토리에서 파일 검색find . -name [FILE]
전체 시스템(루트 디렉토리)에서 파일 검색find / -name [FILE]
파일 이름이 특정 문자열로 시작하는 파일 검색find . -name “STR*”
파일 이름에 특정 문자열이 포함된 파일 검색find . -name “*STR*
파일 이름이 특정 문자열로 끝나는 파일 검색find . -name “*STR”
빈 디렉토리 또는 크기가 0인 파일 검색find . -empty
특정 확장자를 가진 모든 파일 검색 후 삭제find . -name “*.EXT” -delete
검색된 파일 리스트를 줄 바꿈 없이 이어서 출력하기find . -name [FILE] -print0
파일 또는 디렉토리만 검색하기find . -name [FILE] -type f
파일 크기를 사용하여 파일 검색find . -size +[N]c -and -size -[M]c
검색된 파일에 대한 상세 정보 출력. (find + ls)find . -name [FILE] -exec ls -l {} \;
검색된 파일의 라인 수 출력. (find + wc)find . -name [FILE] -exec wc-l {} \;
검색된 파일에서 문자열 찾기. (find + grep)find . -name [FILE] -exec grep “STR” {} \;
검색 결과를 파일로 저장. (find, redirection)find . -name [FILE] > [SAVE_FILE]
검색 중 에러 메시지 출력하지 않기 (find, redirection)find . -name [FILE] 2> /dev/null
하위 디렉토리 검색하지 않기find . -maxdepth 1 -name [FILE]
검색된 파일 복사. (find + cp)find . -name [FILE] -exec cp {} [PATH] \;

 

하나씩 사용해 보면서 결과를 보겠습니다.

 

find

[root@localhost test]# find
.
./.bash_logout
./.bash_profile
./.bashrc
./data
./test.txt
./test1.txt
./log.txt
./web.log
./session.log
[root@localhost test]#

 

find [PATH]

[root@localhost test]# find data
data
data/TEST
[root@localhost test]#

 

find . -name [NAME]

[root@localhost test]# find . -name "test.txt"
./test.txt

 

find . -name “STR*”

[root@localhost test]# find . -name "te*.txt"
./test.txt
./test1.txt
[root@localhost test]#

 

find . -name “STR

[root@localhost test]# find . -name "*e*"
./.bash_profile
./test.txt
./test1.txt
./web.log
./session.log
[root@localhost test]#

 

find . -name “*.STR”

[root@localhost test]# find . -name "*.txt"
./test.txt
./test1.txt
./log.txt
[root@localhost test]# ^C

 

 

find . -empty

[root@localhost test]#  find . -empty
./data/TEST
[root@localhost test]#

 

find . -name “*.EXT” -delete

[root@localhost test]# find . -name "*.log" -delete
[root@localhost test]# ll
total 16
drwxr-xr-x. 4 test ftp  29 Jun 18 10:57 data
-rw-r--r--  1 root root  2 Jun 18 09:41 log.txt
-rw-r--r--  1 root root  2 Jun 18 09:41 test1.txt
-rw-r--r--  1 root root  2 Jun 18 09:41 test.txt
[root@localhost test]#

 

find . -name “STR” -type f

[root@localhost test]# find . -name "*test*" -type f
./.bash_logout
./test.txt
./test1.txt
./log.txt
[root@localhost test]#

 

find . -name “STR” -type d

[root@localhost test]# find . -name "*t*" -type d
./data
[root@localhost test]#

 

find . -maxdepth 1 -name “STR”

[root@localhost test]# find . -maxdepth 1 -name "test*"
./test.txt
./test1.txt
[root@localhost test]#

 

find 커맨드를 사용해 파일 또는 폴더를 사용하는 방법을 알아봤습니다.

파일이나 폴더를 검색할때 *를 앞이나 뒤 또는 앞뒤에 붙여 지정한 문자가 포함된 파일이나 폴더를 검색할 수 있습니다.

단순히 문자만 지정한 경우에는 완전히 일치하는 파일이나 폴더만을 검색합니다.

find 검색을 할때 가장 중요한 부분이니 주의해야 합니다.

댓글