488 private links
Use the -prune primary. For example, if you want to exclude ./misc
:
find . -path ./misc -prune -o -name '*.txt' -print
To exclude multiple directories, OR them between parentheses.
find . -type d \( -path ./dir1 -o -path ./dir2 -o -path ./dir3 \) -prune -o -name '*.txt' -print
And, to exclude directories with a specific name at any level, use the -name
primary instead of -path
.
find . -type d -name node_modules -prune -o -name '*.json' -print
This didn't work for me until I prefixed my local path wih ./
, e.g. ./name
. This distinction for find might not be obvious to the occasional find user. – sebkraemer
There is clearly some confusion here as to what the preferred syntax for skipping a directory should be.
GNU Opinion
To ignore a directory and the files under it, use -prune
From the GNU find man page
Reasoning
-prune
stops find
from descending into a directory. Just specifying -not -path
will still descend into the skipped directory, but -not -path
will be false whenever find
tests each file.
Issues with -prune
-prune
does what it's intended to, but are still some things you have to take care of when using it.
find
prints the pruned directory.
-
TRUE That's intended behavior, it just doesn't descend into it. To avoid printing the directory altogether, use a syntax that logically omits it.
-prune
only works with -print and no other actions.
-
NOT TRUE.
-prune
works with any action except-delete
. Why doesn't it work with delete? For-delete
to work, find needs to traverse the directory in DFS order, since-delete
will first delete the leaves, then the parents of the leaves, etc... But for specifying-prune
to make sense, find needs to hit a directory and stop descending it, which clearly makes no sense with-depth
or-delete
on.
///
My example:
find -s . -path "./C*" -prune -o -name '*' -type d -maxdepth 2 -print