Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 13 down vote accepted

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
share|improve this answer
I was about to write my solution, but it appears to be exactly this. Personally I'd put the -prune inside the first set of brackets, and the -type f -print in a second set, just to make the and/or operations more obvious, but otherwise this looks spot-on to me. – me_and Feb 9 '12 at 12:35
I improved parentheses (and their reasoning) thanks to @me_and 's comment. – fheub Feb 9 '12 at 12:43
Thanks for helping me with what I was looking for. – Senthil Kumaran Feb 9 '12 at 15:35

Have a look at ack: http://betterthangrep.com/

In addition to having a reasonable set of default excluded folders (.hg is a default exclude for instance), it is easy to exclude new folders:

ack --ignore-dir=.directory search_term

To bring this back to your use case where you are looking for a list of files, you would use the -f option, as in:

ack -f --ignore-dir=.directory

I switched from writing convoluted search/find commands to simple ack ones.

Tip: Put commonly used command line options (excluded folders for instance) into a .ackrc file.

share|improve this answer
Based on the find | less in the question, the OP is looking for a simple list of files. How does this help with that? – Michael Kjörling Apr 1 '15 at 5:58
@MichaelKjörling As I stated in my answer, the -f option provides a list of files. – Brian Leathem Apr 1 '15 at 16:37

you can try

find \( ! -regex '.*/\.directory\(\|/.*\)' \)
share|improve this answer
Pretty damn sure this does nothing like what the OP asked for. – me_and Feb 9 '12 at 12:33
I just thought this is what he mean by .directory – yuyichao Feb 9 '12 at 13:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.