커피닉스, 시스템 엔지니어의 쉼터
  find명령에서 특정 디렉토리 제외하고 찾기(exclude) 작성일 : 2009/07/06 13:30
 
  • 글쓴이 : 좋은진호 ( http://coffeenix.net/ )
  • 조회수 : 37203
     
    제  목 : find명령에서 특정 디렉토리 제외하고 찾기(exclude)
    작성자 : 좋은진호(truefeel, http://coffeenix.net/ )
    작성일 : 2009.7.3(금)

    find 명령을 할 때, 특정 디렉토리 제외하고 find하는 방법은 없느냐고 주위분이 물어오셨다.
    개인적으로는 find에서 특정 디렉토리를 exclude하는 것 대신에 디렉토리를 나열하는 방법을 사용했다. 그러나 디렉토리 갯수가 많고, 같은 depth의 디렉토리가 아니고 하위 특정 디렉토리 몇개를 exclude해야한다면? 옵션을 활용할 수 밖에 없을 것이다.

    man페이지를 확인해보니 -prune옵션이 있다. -prune 옵션은 찾아낸 것이 디렉토리이면 그 디렉토리내에는 find하지 않는다.

     
    -name pattern
           ... 생략 ...
           To ignore a directory and the files under it, use -prune; see an example in the description of -wholename.

    -prune If -depth is not given, true; if the file is a directory, do not descend into it.
           If -depth is given, false; no effect.
     


    1. 예제로 살펴보기

    [예제 1] 2009가 포함된 파일을 찾는다. 단 파일명이 디렉토리에 해당되면 해당 디렉토리 이하는 찾지 않는다.

    $ find (전체 보기)
    ./
    ./check/check_list_2008_12.php
    ./check/check_list_2009_05.php
    ./check/check_list_2009_06.php
    ./2007_10_mobile
    ./2008_01_highlight
    ./2008_01_highlight/view.pl
    ./2008_01_highlight/view.sh
    ./2009_03_MSIE8
    ./2009_03_MSIE8/get_MSIE8.sh
    ./2009_05_apache
    ./2009_05_game
    ./2009_05_game/2009_05.log
    $ find . -name "*2009*" -prune
    ./check/check_list_2009_05.php
    ./check/check_list_2009_06.php
    ./2009_03_MSIE8
    ./2009_05_apache
    ./2009_05_game

    ./2009_05_game/ 디렉토리 이하에 2009_05.log 파일이 있지만, -prune옵션으로 해당 디렉토리 이하는 찾지 않았다.

    [예제 2] ./foo/bar 디렉토리는 제외하고, *.txt 파일을 찾아라.

    $ find . ! \( -path './foo/bar' -prune \) -name "*.txt"
    $ find . ! \( -type d -path './foo/bar' -prune \) -name "*.txt" (보다 정확한 표현)

    [예제 3] ./foo/bar 디렉토리와 ./coffeenix/temp 디렉토리는 제외하고, *.bak 파일을 찾아라.

    $ find . ! \( \( -path './foo/bar' -o -path './coffeenix/temp' \) -prune \) -name "*.bak"
    $ find . ! \( \( -type d -path './foo/bar' -o -path './coffeenix/temp' \) -prune \) -name "*.bak" (보다 정확한 표현)

    [예제 4] ./2008로 시작하는 디렉토리는 제외하고, .*.bak 파일을 찾아라.

    $ find . ! \( -path './2008*' -prune \) -name "*.bak"
    $ find . ! \( -type d -path './2008*' -prune \) -name "*.bak" (보다 정확한 표현)

    -path 대신 -wholename 옵션을 사용해도 된다.

     
    -path pattern
          See -wholename.   The predicate -path is also supported by HP-UX find.

    -wholename pattern
          File name matches shell pattern pattern.  The metacharacters do not treat `/' or `.' specially; so, for example,
                    find . -wholename './sr*sc'
          will  print  an entry for a directory called './src/misc' (if one exists).  To ignore a whole directory tree, use -prune rather
          than checking every file in the tree.  For example, to skip the directory `src/emacs' and all files and directories  under  it,
          and print the names of the other files found, do something like this:
                    find . -wholename './src/emacs' -prune -o -print
     


    2. 참고자료

    * find manpage
      http://unixhelp.ed.ac.uk/CGI/man-cgi?find

    * 유용한 find 명령어 예 모음 (2003, 글 좋은진호)
      http://coffeenix.net/board_view.php?bd_code=36


    커피닉스, 시스템 엔지니어의 쉼터 / URL : http://coffeenix.net/board_view.php?bd_code=1678