Find – How to Exclude Directory

find

I am on Linux (Ubuntu) and I am would like to exclude certain directories (like .hg) when I am doing a

find | less  

I tried the following to exclude the .hg directory from listing, but does not seem to work.

find -type d \( ! -iname \.hg \)
find -type d \( ! -name \.hg \)
find -type d \( ! -iname .hg \)

How do I exclude .directory in a find command

Best Answer

On the research for a similar find solution I discovered the helpful explanation on How to use '-prune' option of 'find' in sh? by Laurence Gonsalves.

You could use something like:

find . \( -type d -name .hg -prune \) -o \( -type f -name "foo" -print \)

or (without the name)

find . \( -type d -name .hg -prune \) -o \( -type f -print \)

The (escaped) parentheses \( and \) group the tests (type and name) and corresponding actions (prune and print, respectively) together left and right of the OR (-o).

Since -o has lower precedence than juxtaposition, you can omit the parentheses if you like. Many find implementations also let you skip the final -print.

find . -type d -name .hg -prune -o -type f -name "foo" -print
Related Question