ablog

不器用で落着きのない技術者のメモ

find で hoge.txt という名前のファイルを探したいが、.svn/ は除きたい場合

find -type f -regex '.*hoge.*' -not -regex '.*svn.*' 

「〜を除く」という条件を指定したい場合、「-not expr」と書けば良い。

find -type f -regex '.*hoge.*' ! -regex '.*svn.*' 

「! expr」でも同じ。


man*1 を見たら、and や or の書き方もあった。

       ! expr True  if  expr  is false.  This character will also usually need
              protection from interpretation by the shell.


       -not expr
              Same as ! expr, but not POSIX compliant.


       expr1 expr2
              Two expressions in a row are taken to be joined with an  implied
              "and"; expr2 is not evaluated if expr1 is false.


       expr1 -a expr2
              Same as expr1 expr2.


       expr1 -and expr2
              Same as expr1 expr2, but not POSIX compliant.


       expr1 -o expr2
              Or; expr2 is not evaluated if expr1 is true.


       expr1 -or expr2
              Same as expr1 -o expr2, but not POSIX compliant.


       expr1 , expr2
              List;  both  expr1 and expr2 are always evaluated.  The value of
              expr1 is discarded; the value of the list is the value of expr2.
              The  comma operator can be useful for searching for several dif-
              ferent types of thing, but traversing the  filesystem  hierarchy
              only  once.  The -fprintf action can be used to list the various
              matched items into several different output files.

参考


追記(2009/10/28):

ファイル名にhogeのつくファイルを探す。ただし、.svn/ 以外。 → find -type f -regex '.*hoge.*' -not -regex '.*svn.*'

yohei.az on Twitter: "ファイル名にhogeのつくファイルを探す。ただし、.svn/ 以外。 → find -type f -regex '.*hoge.*' -not -regex '.*svn.*'"

とつぶやいたら、id:teramako さんから

@yoheia ノー!!! http://d.hatena.ne.jp/teramako/20091002/p1

てらまこ on Twitter: "@yoheia ノー!!! http://d.hatena.ne.jp/teramako/20091002/p1"

とツッコミが入ったので、特定ディレクトリ以下を除いた find をする方法 - hogehoge @teramako を読んでみた。

find -name '.svn' -prune -o -regex '.*hoge.*' -print 

としたほうがベターのようだ。
最初の書き方だとパスに svn を含むディレクトリ・ファイルが排除されるが、この書き方だと、.svn というディレクトリの配下のファイルを除外する。

*1:これは cygwin の man