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
Join Stack Overflow to learn, share knowledge, and build your career.
|
Use the prune switch, for example if you want to exclude the
Here is an example with multiple directories:
Here we exclude dir1, dir2 and dir3, since in |
|||||||||||||||||||||
|
|
If
|
|||||||||||||||||||||
|
|
I find the following easier to reason about than other proposed solutions:
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. Inside One might ask if adding That is also easy to expand to add additional exclusions. For example:
|
|||||||||||||||||||||
|
|
There is clearly some confusion here as to what the preferred syntax for skipping a directory should be. GNU Opinion
Reasoning
Issues with
Performance I set up a simple test of the three top upvoted answers on this question (replaced
Conclusion Both f10bit's syntax and Daniel C. Sobral's syntax took 10-25ms to run on average. GetFree's syntax, which doesn't use Note Daniel C. Sobral's syntax performed the better of the two Test Script
|
|||||||||||||
|
|
One option would be to exclude all results that contain the directory name with grep. For example:
|
|||||||||||||||||||||
|
|
I prefer the
|
|||||||||||||||||||||
|
|
Use the -prune option. So, something like:
The '-type d -name proc -prune' only look for directories named proc to exclude. |
|||||||||
|
|
This is the format I used to exclude some paths:
I used this to find all files not in ".*" paths:
|
|||
|
|
|
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 |
||||
|
|
|
To exclude multiple directories:
To add directories, add
But maybe you should use a regular expression, if there are many directories to exclude. |
||||
|
|
|
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. |
|||||
|
|
I was using Although I was able to ignore the contents of the directory which I wanted ignored, My solution was to simply use
Whether or not there is an argument for |
|||
|
|
seems to work the same as
and is easier to remember IMO. |
|||
|
|
|
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:
|
|||
|
|
|
|||||
|
|
None of previous answers is good on Ubuntu. Try this:
I have found this here |
|||||||||
|
|
This is suitable for me on a Mac:
It will exclude |
||||
|
|
|
The tricky part is that
Here's a working example:
|
||||
|
|
|
how-to-use-prune-option-of-find-in-sh is an excellent answer by Laurence Gonsalves on how And here is the generic solution:
To avoid typing
|
||||
|
|
|
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:
|
||||
|
|
|
Better use the
The Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation*. Notes* From the EXAMPLES section of the |
|||||||||||||||||
|
|
This works because
but it does NOT work if you don't use a pattern (
There is no
or:
Regards |
|||
|
|
|
For FreeBSD users:
|
|||||||||
|
|
If search directories has pattern (in my case most of the times); you can simply do it like below:
In above example; it searches in all the sub-directories starting with "n". |
|||
|
|
|
I have found the suggestions on this page and a lot of other pages just do not work on my Mac OS X system. However, I have found a variation which does work for me. The big idea is to search the Macintosh HD but avoid traversing all the external volumes, which are mostly Time Machine backups, image backups, mounted shares, and archives, but without having to unmount them all, which is often impractical. Here is my working script, which I have named "findit".
The various paths have to do with external archive volumes, Time Machine, Virtual Machines, other mounted servers, and so on. Some of the volume names have spaces in them. A good test run is "findit index.php", because that file occurs in many places on my system. With this script, it takes about 10 minutes to search the main hard drive. Without those exclusions, it takes many hours. |
|||
|
|
|
i wanted to know the number of directories, files an MB of just the current directory - and that code does exactly what i want :-) the source
the code
note: the extra the result
|
|||
|
|
|
Not sure if this would cover all edge cases, but following would be pretty straight forward and simple to try:
This should remove all files/directories from the current directory excpet 'ddls' and 'docs'. |
|||
|
|
|
I tried command above, but none of those using "-prune" works for me. Eventually I tried this out with command below:
|
||||
|
|
|
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 ... | while read -r file .... Also, it's better to accept and upvote answers. – Dennis Williamson Nov 17 '10 at 23:26for file in $(find .); do echo "$file"; done. Names with spaces are split, which we don't want. – Jean-Philippe Pellet Dec 2 '15 at 10:09