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.<
find -name '*.js' -not -path './node_modules/*' -not -path './vendor/*'
seems to work the same as
find -name '*.js' -not \( -path './node_modules/*' -o -path './vendor/*' \)
and is easier to remember IMO.
For those of you on older versions of UNIX who cannot use -path or -not
Tested on SunOS 5.10 bash 3.2 and SunOS 5.11 bash 4.4
find . -type f -name "*" -o -type d -name "*excluded_directory*" -prune -type f
This is the only one that worked for me.
find / -name MyFile ! -path '*/Directory/*'
Searching for "MyFile" excluding "Directory". Give emphasis to the stars * .
I found the functions name in C sources files exclude *.o and exclude *.swp and exclude (not regular file) and exclude dir output with this command:
find . \( ! -path "./output/*" \) -a \( -type f \) -a \( ! -name '*.o' \) -a \( ! -name '*.swp' \) | xargs grep -n soc_attach
You can use the prune option to achieve this. As in for example:
find ./ -path ./beta/* -prune -o -iname example.com -print
Or the inverse grep “grep -v” option:
find -iname example.com | grep -v beta
You can find detailed instructions and examples in Linux find command exclude directories from searching.
Better use the exec
action than the for
loop:
find . -path "./dirtoexclude" -prune \
-o -exec java -jar config/yuicompressor-2.4.2.jar --type js '{}' -o '{}' \;
The exec ... '{}' ... '{}' \;
will be executed once for every matching file, replacing the braces '{}'
with the current file name.
Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation*.
* From the EXAMPLES section of the find (GNU findutils) 4.4.2
man page