커피닉스, 시스템 엔지니어의 쉼터 커피향이 나는 *NIX
커피닉스
시스템/네트웍/보안을 다루는 곳
* HanIRC의 #coffeenix 방
[ 장비 및 회선 후원 ]
HOME > Unix/Linux 처음 시작하기 > 명령어 사용하기 도움말
검색 : 사이트 WHOIS 웹서버 종류


  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
      커피닉스 카페 최근 글
    [06/16] [질문]SAN 스위치 브로드케이드 dcx8510-4 장비관련입니다.
    [04/22] Re: 영화속에 컴퓨터 이야기 ~스워드피쉬(2001)
    [10/20] Cross Compiler 깔
    [07/14] SSL АО
    [04/26] Re: 도스화면 원격조종 여부
    [04/25] 도스화면 원격조종 여부
    [10/30] Cshell에서 난수 설정
    [10/23] 공항철도주식회사 SE 구인 件
    [01/26] Re: wget으로 다른서버에있는 디렉토리를 가져오려고합니다.
    [01/25] wget으로 다른서버에있는 디렉토리를 가져오려고합니다.
    [01/11] 특정 안드로이드 WebView 버전에서 SSL 문제 (WebView 버그)
    [08/01] DNS forwarder (전달자) 서버를 통해서 쿼리하면 역방향을 받아오질 못합니다.
    [05/16] (주)후이즈 시스템엔지니어 (경력자) 모집
    [02/15] [AWS] Cloudfront edge 확인하기
    [01/20] Mobile Service/eCommerce 기업에서 Server / Java / PHP 개발자 구인
      New!   최근에 등록한 페이지
      KiCad EDA Suite project (Free/Libre/Open-Source EDA Suite) (CAD)
      오픈캐스케이드 캐드 (OpenCASCADE CAD)
      QCad for Windows --- GNU GPL (Free Software)
      The Hello World Collection
      IPMI를 활용한 리눅스 서버관리
      DNS 설정 검사
      nagiosgraph 설치 방법
      Slony-I 설치 방법 (postgresql replication tool)
      Qmail기반의 Anti spam 시스템 구축하기
      clusterssh

    [ 함께하는 사이트 ]




    운영진 : 좋은진호(truefeel), 야수(yasu), 범냉이, sCag
    2003년 8월 4일~