This question already has an answer here:

I am using find -type f command to recursively find all files from a certain starting directory. However, I would like to have some directories prevented from entering and extracting names of files inside. So basically I am looking for something like:

find . -type f ! -name "avoid_this_directory_please"

Is there a functioning alternative to this?

marked as duplicate by strugee, sam, thrig, don_crissti, mdpc Mar 10 '17 at 1:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    There's a dozen questions on the troubles with using -prune: it can be a bit confusing. Also this on stackoverflow describes why it's such an annoying thing to use. – ilkkachu Mar 8 '17 at 21:26
  • 1
    Even if this is a dupe the best answer here is clearer than the best answer on the "correct" question. – chicks Oct 9 '18 at 16:04

This is what the -prune option is for:

find . -type d -name "avoid_this_directory_please" -prune -o -type f -print

This basically says "if there's a directory called avoid_this_directory_please, don't enter it, otherwise, if it's a file, print its name".

  • 2
    i was going to write the same thing, slightly differently, as find . \( -type d -name 'avoid_this_directory_please' -prune \) -o \( -type f -print \), but I like your syntax better. Thanks. – Tim Kennedy Mar 8 '17 at 21:20
  • 2
    @TimKennedy You would probably need the parens if you wanted to do find . -type d \( -name "A" -o -name "B" \) -prune -o -type f -print. – Kusalananda Mar 8 '17 at 21:22
  • good point. i don't even know when/where I learned to do it with the parentheses. that age is lost in the mists of time. :) – Tim Kennedy Mar 8 '17 at 21:23
  • 1
    This will of course prune off any directories called avoid_this_directory_please anywhere in the tree, not just top level ones. -path would allow for more control in that, as the man page (quoted below by Xen2050) states. – ilkkachu Mar 8 '17 at 21:30

To avoid a directory try the -path test:

find . -type f ! -path '*/avoid_this_directory_please/*'
  • 4
    Note that while it will prevent files inside avoid_this_directory_please from being selected (unless their path contain invalid characters), it will not stop find from descending in there. That's why -prune is better here. – Stéphane Chazelas Mar 8 '17 at 21:22

Try this

find -name "*.js" -not -path "./directory/*"
  • This works if only you are looking for one extension type. The following will fail: find -name "*.js" -o -name "*.css" -not -path "./directory/*" – TheBicentennialMan Dec 4 '17 at 10:28

From man find, in the -path section, apparently combining the other 2 answers

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 . -path ./src/emacs -prune -o -print

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