'findlinux'에 해당되는 글 1건

  1. 2020.11.17 [linux]find 명령어
Linux2020. 11. 17. 11:59

 

 

 

find 명령어는 특정 파일이나 디렉토리를 검색한다.

기본적으로는 이런 형식을 많이 쓴다.

예)find [검색경로] -name [파일명]

파일을 풀네임으로 검색할수도있고, 조건으로 검색할 수도 있다.

exec 옵션을 사용해서 다음 행동까지 이어서 할 수도 있고, wc -l로 숫자를 세는 등 다양하게 연결해서 명령어를 입력할 수 있다.

사용 예)

 

[root@localhost~]$ find ./ -name 'fileDEER' //현재경로에서 찾기
[root@localhost~]$ find / -name 'fileDEER' //전체 경로에서 찾기
[root@localhost~]$ find / -name '*.txt' //확장자가 .txt로 끝나는 파일 찾기
[root@localhost~]$ find ./ -name '*.txt' -exec rm {} \; //현재 디렉토리에서 확장자가 .txt로 
                                                        //끝나는 파일 삭제
[root@localhost~]$ find ./ -type d //디렉토리만 검색
[root@localhost~]$ find ./ -type f //파일만 검색
[root@localhost~]$ find ./ -type f | wc -l //특정 디렉토리에 find 조건에 맞는 결과값이 몇개 
                                           //존재하는지 확인 가능
[root@localhost~]$ find //현재 디렉토리에 있는 파일 및 디렉토리 리스트 표시
[root@localhost~]$ find . -name [FILE] //현재 디렉토리 아래 모든 파일 및 하위 디렉토리에서 파일 검색
[root@localhost~]$ find . -name 'str*' //파일이름이 'str'로 시작하는 파일 검색
[root@localhost~]$ find . -name '*str' //파일 이름이 특정 문자열로 끝나는 파일 검색
[root@localhost~]$ find . -name '*.txt" -delete //.txt로 끝나는 확장자를 가진 모든 파일 검색 후 삭제
[root@localhost~]$ find . -name [FILE] -print0 //검색된 파일 리스트를 줄바꿈없이 이어서 출력
[root@localhost~]$ find . -size +[N]c -and -size -[M]c //파일 크기를 사용하여 파일 검색
[root@localhost~]$ find . -name [FILE] -exec ls -l {} \;//검색된 파일에 대한 상세 정보 출력(find + ls)
[root@localhost~]$ find . -name [FILE] -exec wc-l {} \;//검색된 파일의 라인수 출력
[root@localhost~]$ find . -name [FILE] -exec grep "str" {} \; //검색된 파일에서 문자열 찾기
[root@localhost~]$ find . -name [FILE] > [SAVE_FILE] //검색 결과를 파일로 저장(find, redirection)
[root@localhost~]$ find . -name [FILE] 2> /dev/null //검색중 에러 메시지 출력하지 않기(find, redirection)
[root@localhost~]$ find . -maxdepth 1 -name [FILE] //하위 디렉토리 검색하지 않기
[root@localhost~]$ find . -name [FILE] -exec cp {} [PATH] \; //검색된 파일 복사(find + cp)

 

find 명령어 옵션)

OPTION

-P : 심볼릭 링크를 따라가지 않고, 심볼릭 링크 자체 정보 사용

-L : 심볼릭 링크에 연결된 파일 정보 사용

-H : 심볼릭 링크를 따라가지 않으나, Command Line Argument를 처리할 땐 예외

-D : 디버그 메시지 출력

EXPRESSION

-name : 지정된 문자열 패턴에 해당하는 파일 검색

-empty : 빈 디렉토리 또는 크기가 0인 파일 검색

-delete : 검색된 파일 또는 디렉토리 삭제

-exec : 검색된 파일에 대해 지정된 명령 실행

-path : 지정된 문자열 패턴에 해당하는 경로에서 검색

-print : 검색결과를 출력. 검색항목은 newline으로 구분(기본 값)

-print0 : 검색 결과를 출력. 검색 항목은 null로 구분

-size : 파일 크기를 사용하여 파일 검색

-type : 지정된 파일 타입에 해당하는 파일 검색

-mindepth : 검색을 시작할 하위 디렉토리 최소 깊이 지정

-maxdepth : 검색할 하위 디렉토리의 최대 깊이 지정

-atime : 파일 접근(access) 시각을 기준으로 파일 검색

-ctime : 파일 내용 및 속성 변경(change) 시간을 기준으로 파일 검색

-mtime : 파일의 데이터 수정(modify) 시각을 기준으로 파일 검색

덧붙여, 연산자(Operator)를 사용하여 두개 이상의 표현식 조합도 가능하다.

(expression) : expression 우선순위 지정

!expression 혹은 -not expression : expression 결과에 NOT 연산

expression -a expression

expression -and expression

expression expression : expression 간 AND 연산

expression -o expression

expression -or expression : expression간 OR 연산

*연산자를 지정하지 않으면, 기본적으로 -a(AND)가 적용된다.

 

 

'Linux' 카테고리의 다른 글

[Linux]tar, gz, zip 압축 및 해제  (0) 2020.11.17
[linux]grep 명령어  (0) 2020.11.17
[linux]mv 명령어  (0) 2020.11.15
[linux]touch 명령어  (0) 2020.11.15
[linux]rmdir 명령어  (0) 2020.11.15
Posted by 사슴영혼'-'