I'm trying to run a find
command for all JavaScript files, but how do I exclude a specific directory?
Here is the find
code we're using.
for file in $(find . -name '*.js')
do
java -jar config/yuicompressor-2.4.2.jar --type js $file -o $file
done
The -path -prune approach also works with wildcards in the path. Here is a find statement that will find the directories for a git server serving multiple git repositiories leaving out the git internal directories:
I prefer the
-not
notation ... it's more readable:For a working solution (tested on Ubuntu 12.04 (Precise Pangolin))...
will search for MP3 files in the current folder and subfolders except in dir1 subfolder.
Use:
...to exclude dir1 AND dir2
-prune
definitely works and is the best answer because it prevents descending into the dir that you want to exclude.-not -path
which still searches the excluded dir, it just doesn't print the result, which could be an issue if the excluded dir is mounted network volume or you don't permissions.The tricky part is that
find
is very particular about the order of the arguments, so if you don't get them just right, your command may not work. The order of arguments is generally as such:{path}
: Put all the path related arguments first, like. -path './dir1' -prune -o
{options}
: I have the most success when putting-name, -iname, etc
as the last option in this group. E.g.-type f -iname '*.js'
{action}
: You'll want to add-print
when using-prune
Here's a working example:
None of previous answers is good on Ubuntu. Try this:
I have found this here
You can use the prune option to achieve this. As in for example:
Or the inverse grep “grep -v” option:
You can find detailed instructions and examples in Linux find command exclude directories from searching.