
not \( -path "*/prefix*" -type d -prune \) | sort -V

# prefix "prefix" at any level recursively within the specified directory.įind. # Ignore all directories (and their contents, via `-prune`) beginning with not \( -path "./prefix*" -type d -prune \) | sort -V # prefix "prefix" at the lowest level of the specified directory (`.`).įind. Here is how: # Ignore all directories (and their contents, via `-prune`) beginning with How can I ignore directories with a given prefix? For example, I would like to exclude directories starting with _. This is a really useful example that doesn't answer the OP's question directly, but is even more useful in my Dziedzic asked in a comment below my answer (corrected for grammar and punctuation): How do I exclude a specific directory when searching for *.js files using find? Quick example: exclude all directories with a given prefix Printf " $name3_num $name3_perf\n\n"įind is incredibly important and powerful, but it is so nuanced and confusing! I set up a simple test of the three top upvoted answers on this question (replaced -print with -exec bash -c 'echo $0' \\\

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. Why doesn't it work with delete? For -delete to work, find needs to traverse the directory in DFS order, since -deletewill first delete the leaves, then the parents of the leaves, etc. prune works with any action except -delete. prune only works with -print and no other actions. To avoid printing the directory altogether, use a syntax that logically omits it.

If on the other hand you run find like this cd /tmp find. Note : If you want to exclude /tmp/foo/bar and you run find like this " find /tmp \(." then you must specify -path /tmp/foo/bar. This comes from an actual use case, where I needed to call yui-compressor on some files generated by wintersmith, but leave out other files that need to be sent as-is. The way -prune works is that anything that, once it is reached, the files below that directory are permanently ignored. One might ask if adding -not will not make all other files hidden by -prune reappear, and the answer is no. This is then grouped as a single expression with the escaped parenthesis, and prefixed with -not which will make find skip anything that was matched by that expression. Inside \( and \) is an expression that will match exactly build/external (see important note above), and will, on success, avoid traversing anything below. See note if you'd like a better understanding.
#FIND FILE LINUX RECURSEVILY FULL#
If this sentence confuses you just make sure to use full paths through out the whole command like this: find /full/path/ -not \( -path /full/path/exclude/this -prune \). Important Note: the paths you type after -path must exactly match what find would print without the exclusion.

I find the following easier to reason about than other proposed solutions: find build -not \( -path build/external -prune \) -name \*.jsįind build -not \( -path build/external -prune \) -not \( -path build/blog -prune \) -name \*.js
