본문으로 바로가기

: 디렉토리 안의 파일 검색시 사용하는 명령어

Syntax : find [options] [path] [expression]

Manual page

 

목차

     

    0> 도표

    옵션
    의미
    기타
    -P
    심볼릭 링크 따라가지 않는 옵션
    목차 참조
    -L
    심볼릭 링크 따라가는 옵션
    목차 참조
    -H
    심볼릭 링크 따라가지 않는 옵션
    목차 참조
    -D debugoptions
    진단 정보를 출력
    목차 참조
    -maxdepth levels
    최대 디렉토리 깊이 설정
    목차 참조
    -mindepth levels
    최소 디렉토리 깊이 설정
    목차 참조
    -name pattern
    패턴이 들어간 파일 검색
    목차 참조
    -iname pattern
    -name 과 비슷 (대소문자 구별안함.)
    목차 참조
    -perm mode
    퍼미션이 mode 인 파일 검색
    목차 참조
    -type c
    c 에 지정된 타입의 파일/디렉토리 검색
    목차 참조
    -print0
    newline 대신 null 사용옵션
    목차 참조
    -ls
    ls 옵션 설정
    목차 참조
    -exec command {} \;
    추가 명령어 실행옵션
    목차 참조

     

     

    옵션

    -P 옵션 (옵션 미사용시도 적용됨 : at default)

    : 심볼릭 링크를 따라가지 않는 옵션

     

    -L 옵션

    : 심볼릭 링크를 따라가서 검색하는 옵션

    $ tree
    .
    ├── a.txt
    ├── b.txt
    ├── c.txt
    ├── dir1
    │   ├── a.txt
    │   └── b.txt
    ├── dir1.lnk -> dir1
    └── dir2
    
    >> 심볼릭 링크 따라가지 않는 옵션 테스트  (-P)
    $ find -P -name a.txt
    ./dir1/a.txt
    ./a.txt
    
    >> 심볼릭 링크 따라가는 옵션 테스트  (-L)
    $ find -L -name a.txt
    ./dir1.lnk/a.txt           <----- 심볼릭 링크
    ./dir1/a.txt
    ./a.txt

     

    -H 옵션

    : 심볼릭 링크를 따라가지 않는 옵션 ( 명령 라인 아규먼트 제외)

    -D 옵션 (debugoptions)

    : 진단 정보를 출력

    >> debugoptions ==> help , tree ,stat, opt ,rates

    매뉴얼 페이지

    $ find -D tree -name a.txt
    Predicate List:
    [(] [-name] [)] [-a] [-print] 
    Eval Tree:
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    left:
        pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
        no children.
    right:
        pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
        no children.
    Normalized Eval Tree:
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    left:
        pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
        no left.
        right:
            pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
            no children.
    right:
        pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
        no children.
    Optimized Eval Tree:
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    left:
        pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
        no left.
        right:
            pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
            no children.
    right:
        pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
        no children.
    Optimized command line:
    -name a.txt [0.1] -a [0.1] -print [1] 
    ./dir1/a.txt
    ./a.txt
    
    
    $ find -D stat -name a.txt
    ./dir1/a.txt
    ./a.txt
    
    
    $ find -D opt -name a.txt
    Predicate List:
    [(] [-name] [)] [-a] [-print] 
    Eval Tree:
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    left:
        pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
        no children.
    right:
        pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
        no children.
    Normalized Eval Tree:
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    left:
        pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
        no left.
        right:
            pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
            no children.
    right:
        pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
        no children.
    -O0: promoting cheap predicate -name a.txt into name_list
    predlist before merge sort:
            pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
            no left.
            right:
                pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
                no children.
    predlist after merge sort:
            pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
            no left.
            right:
                pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
                no children.
    Not an arm swap candidate (Right subtree has side-effects):
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    left:
        pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
        no left.
        right:
            pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
            no children.
    right:
        pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
        no children.
    Not an arm swap candidate (Doesn't have two arms):
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    no left.
    right:
        pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
        no children.
    Not an arm swap candidate (Not a binary operation):
    pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
    no children.
    Not an arm swap candidate (Not a binary operation):
    pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
    no children.
    Optimized Eval Tree:
    pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
    left:
        pred=[-a] type=bi_op prec=and cost=Nothing rate=0.10 no side effects 
        no left.
        right:
            pred=[-name a.txt] type=primary prec=no cost=Nothing rate=0.10 no side effects 
            no children.
    right:
        pred=[-print] type=primary prec=no cost=Nothing rate=1.0 side effects 
        no children.
    Optimized command line:
    -name a.txt [0.1] -a [0.1] -print [1] 
    ./dir1/a.txt
    ./a.txt
    
    
    $ find -D rates -name a.txt
    ./dir1/a.txt
    ./a.txt
    Predicate success rates after completion:
    -name a.txt [0.1] [2/9=0.222222] -a [0.1] [2/9=0.222222] -print [1] [2/2=1]

     

    -maxdepth 옵션

    : 최대 디렉토리 깊이 설정 옵션

    >> 현재 디렉토리 아래 1개의 서브디렉토리 까지에서 파일 찾기

    $ find . -maxdepth 2 -type f

    >> maxdepth 1 : 현재 디렉토리를 의미함.

    $ find . -maxdepth 1 -type f

    ./license.txt

    -mindepth 옵션

    : 최소 디렉토리 깊이 설정 (현재 디렉토리는 mindepth : 1)

    $ tree
    .
    ├── a.txt
    ├── dir1
    │   ├── a.txt
    │   ├── subdir1
    │   │   ├── a.txt
    │   ├── subdir2
    │   │   ├── a.txt
    │   └── subdir3
    └── dir2
        ├── subdir1
        │   └── a.txt
        ├── subdir2
        │   └── a.txt
        └── subdir3
    
    >> 최소 디렉토리 깊이 3 이상 설정
    $ find -mindepth 3 -name  a.txt
    ./dir1/subdir2/a.txt
    ./dir1/subdir1/a.txt
    ./dir2/subdir2/a.txt
    ./dir2/subdir1/a.txt
    
    >> 최소 디렉토리 깊이 2 이상 설정
    $ find -mindepth 2 -name  a.txt
    ./dir1/subdir2/a.txt
    ./dir1/a.txt
    ./dir1/subdir1/a.txt
    ./dir2/subdir2/a.txt
    ./dir2/subdir1/a.txt

     

    -name 옵션 (pattern)

    : pattern 으로 지정한 파일 이름 검색 옵션

    -iname 옵션

    : -name 옵션과 비슷하나 대소문자를 구별하지 않습니다.

    -perm 옵션

    : 파일의 퍼미션 비트 매칭 옵션

    $ tree -p
    .
    ├── [-rw-rw-r--]  a.txt
    ├── [-rw-rw-r--]  b.txt
    ├── [-rw-rw-r--]  c.txt
    ├── [drwxrwxr-x]  dir1
    │   ├── [-rw-rw-r--]  a.txt
    │   ├── [-rw-rw-r--]  b.txt
    │   ├── [drwxrwxr-x]  subdir1
    │   │   ├── [-rw-rw-r--]  a.txt
    │   │   └── [-rw-rw-r--]  c.txt
    │   ├── [drwxrwxr-x]  subdir2
    │   │   ├── [-rw-rw-r--]  a.txt
    │   │   ├── [-rw-rw-r--]  b.txt
    │   │   └── [-rw-rw-r--]  c.txt
    │   └── [drwxrwxr-x]  subdir3
    ├── [lrwxrwxrwx]  dir1.lnk -> dir1
    └── [drwxrwxr-x]  dir2
        ├── [drwxrwxr-x]  subdir1
        │   └── [-rw-rw-r--]  a.txt
        ├── [drwxrwxr-x]  subdir2
        │   └── [-rw-rw-r--]  a.txt
        └── [drwxrwxr-x]  subdir3
    
    9 directories, 12 files
    
    $ find -perm 775
    .
    ./dir1
    ./dir1/subdir2
    ./dir1/subdir1
    ./dir1/subdir3
    ./dir2
    ./dir2/subdir2
    ./dir2/subdir1
    ./dir2/subdir3
    
    $ find -perm 664
    ./dir1/subdir2/c.txt
    ./dir1/subdir2/a.txt
    ./dir1/subdir2/b.txt
    ./dir1/a.txt
    ./dir1/subdir1/c.txt
    ./dir1/subdir1/a.txt
    ./dir1/b.txt
    ./c.txt
    ./a.txt
    ./dir2/subdir2/a.txt
    ./dir2/subdir1/a.txt
    ./b.txt

     

    -type 옵션

    : directory (d) , regular file(f) , symbolic link (l) , socket (s) 등의 파일 검색 옵션

    >> 현재 디렉토리 아래있는 모든 디렉토리를 찾기.
    $ find -type d
    .
    ./dir1
    ./dir1/subdir2
    ./dir1/subdir1
    ./dir1/subdir3
    ./dir2
    ./dir2/subdir2
    ./dir2/subdir1
    ./dir2/subdir3

     

     

    -print0 옵션

    : newline 대신 널 문자를 이용해 파일들을 구분해 출력하는 옵션

    매뉴얼 페이지

     

    $ sudo find /etc -name 'host*'
    /etc/hosts
    /etc/avahi/hosts
    /etc/hosts.allow
    /etc/hostname
    /etc/init/hostname.conf
    /etc/host.conf
    /etc/hosts.deny
    
    >> 개행이 없이 
    $ sudo find /etc -name 'host*' -print0
    /etc/hosts/etc/avahi/hosts/etc/hosts.allow/etc/hostname/etc/init/hostname.conf/etc/host.conf/etc/hosts.deny
    
    >> wc 명령과 결합해서 사용 한경우
    $ sudo find /etc -name 'host*' -print0 | wc -c --files0-from=-
    227 /etc/hosts
    1121 /etc/avahi/hosts
    425 /etc/hosts.allow
    13 /etc/hostname
    284 /etc/init/hostname.conf
    92 /etc/host.conf
    711 /etc/hosts.deny
    2873 total

     

     

    -exec 옵션

    : 추가 명령 실행옵션

    {} ==> 검색된 내용이 하나씩 들어갑니다.

    ; ==> 명령어를 실행하라는 의미로 이전에 들어오는 데이타는 아규먼트로 인식

    아규먼트와 구별하기 위해 \ 를 추가후 사용합니다. (\;)

    $ find -name a.txt
    ./dir1/subdir2/a.txt
    ./dir1/a.txt
    ./dir1/subdir1/a.txt
    ./a.txt
    ./dir2/subdir2/a.txt
    ./dir2/subdir1/a.txt
    
    >> a.txt 파일을 찾고 head 명령을 통해서 안의 내용을 출력합니다.
    $ find -name a.txt -exec head -v {} \;
    ==> ./dir1/subdir2/a.txt <==
    hello
    ==> ./dir1/a.txt <==
    hello
    ==> ./dir1/subdir1/a.txt <==
    hello
    ==> ./a.txt <==
    hello
    ==> ./dir2/subdir2/a.txt <==
    hello
    ==> ./dir2/subdir1/a.txt <==
    hello
    
    
    >> 현재 디렉토리 안의 파일을 찾고  확장자로 .txt 추가하기
    $ find -type f
    ./a02
    ./test02
    ./a01
    ./test03
    ./test01
    ./b01
    
    $ find -type f -exec mv {} {}.txt \;
    $ ls
    a01.txt  a02.txt  b01.txt  test01.txt  test02.txt  test03.txt

     

    < 기타>

    drivers directory 아래의 파일 중 "serial " 단어가 들어간 파일 찾기

    $ find drivers/ -name *serial*

    >> directory 가 아닌 파일이름에 serial 이란 문자열이 있는 파일을 찾아줍니다.

    >> 확장자가 txt 인 파일 찾기

    $ find . -name *.txt

    or

    $ find . -name "*.txt"

    or

    $ find . -name "*.txt" -type f

    현재 디렉토리 아래의 파일중 e.txt 와 f.txt 파일 찾기

    $find . -name '[e|f].txt'

     

     

    현재 디렉토리 위 위 위 디렉토리 에서 nrf_uart.h 파일 검색하기

    $ find ../../../ -name nrf_uart.h

    $ find ../../../ -name nrf_uart.h
    ../../../modules/nrfx/hal/nrf_uart.h

     

    find . -cmin -600 -type f | grep -v git | xargs ls -l $1

    >> 600분 이전 파일 검색 (find . -cmin -600 -type f )

    git 이란 이름이 포함이 안되야 되고 ( grep -v git)

    ls 를 사용해 list 형식으로 출력하기 ( xargs ls -l $1)

    find . -mtime -1 -type f | grep -v .git

    >> 하루안에 만든 파일 찾기 (.git 이 포함된 파일 제외)

    find . -mtime +10 -type f | grep -v .git

    >> 10일 이상된 파일 찾기

    find . -mtime 10 -type f | grep -v .git

    >> 10일 된 파일 찾기

    time 관련 Manual page

    현재 디렉토리 아래의 모든 파일을 찾고 몇개인지 확인하기

    $ find -type f | wc -l

    find .git/objects/ -mmin -170 -type f

    >> .git/objects/ 아래의 파일중 170분 이전에 만든 파일찾기

    find .git/objects/ -mmin +180 -type f

    180분 이후에 생성된 파일 찾기

    find .git/objects/ -mmin 179 -type f

       : 정확히 179분 된 파일 찾기

    $ find .git/objects/ -mmin 179 -type f
    .git/objects/78/310d62a46e9e8c24f50800af084d30144fc6a6
    .git/objects/97/12052c02c64805aefa00b397b1f88c552ffed4
    
    $ ll .git/objects/78/310d62a46e9e8c24f50800af084d30144fc6a6
    -r--r--r-- 1 leevisual 197121 59 8월  18 11:28 .git/objects/78/310d62a46e9e8c24f50800af084d30144fc6a6
    
    $ date
    2021년 08월 18일 수 오후  2:26:40

     

    >> 179분 00초 <= files<180분

    -mmin manual page

    find . -maxdepth 1 -name ".?*"

       : 현재 디렉토리안의 숨긴파일 찾기

    $ find .  -maxdepth 1 -name ".?*" 
    ./.jack-settings
    ./.local
    ./.gradle
    ./.cache
    ./.repoconfig
    ./.repopickle_.gitconfig
    ./.vim
    ~~~~ 중략 ~~~~

     

    현 디렉토리 안의 symbolic link 파일을 찾고 연결된 파일 리스트하기

    >> 현재 디렉토리안의 심볼릭 링크 파일 출력하기
    $ find -type l 
    ./apparmor_status
    ./reject
    ./tcptraceroute
    ~~~~~ 중략 ~~~~
    
    >> 심볼릭 링크파일에 -ls 옵션을  추가해 연결파일 찾기 
    $ find -type l -ls
    26744114    0 lrwxrwxrwx   1 root     root            9  9월 28  2018 ./apparmor_status -> aa-status
    26744374    0 lrwxrwxrwx   1 root     root           10 11월 19  2018 ./reject -> cupsaccept
    26740421    0 lrwxrwxrwx   1 root     root           31  8월  3  2018 ./tcptraceroute -> /etc/alternatives/tcptraceroute
    ~~~~~ 중략 ~~~~
    
    >> 심볼릭 링크파일에 -ls 옵션을  추가해 연결파일 찾기 
       앞의 필요없는 부분 잘라내기 (cut -b )
    $ find -type l -ls | cut -b 71-
     ./apparmor_status -> aa-status
     ./reject -> cupsaccept
     ./tcptraceroute -> /etc/alternatives/tcptraceroute
    ~~~~~ 중략 ~~~~

     

    -ls 옵션 매뉴얼

     

     

    그럼 수고하세요.

     

     

     

     

     

    반응형