- Exclude a directory or multiple directories while using find command
- Method 1 : Using the option “-prune -o”
- Method 2 : Using “! -path”
- Method 3 : Simple 🙂
- Excluding multiples directories
- Exclude list of files from find
- 6 Answers 6
- Not the answer you’re looking for? Browse other questions tagged linux shell find or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Use find command but exclude files in two directories
- 6 Answers 6
- Explanation
- Find command Exclude or Ignore Files (e.g. Ignore All Hidden .dot Files )
- Find command exclude or ignore files syntax
- Examples: find command and logical operators
- Understanding find command operators
- How do I ignore hidden .dot files while searching for files?
- Say hello to -path option
- How do I make «find» exclude the folder it searches in?
- 3 Answers 3
Exclude a directory or multiple directories while using find command
Table of Contents
Is it possible to exclude a directory with find command? Exclude directories while doing running find command?
Yep, the command FIND has wide range of options to search what you actually looking for. I have already listed different switches and its usages with examples. Here we go for excluding some directories from our find job.
In some cases, we have to exclude some directories from our search pattern to improve the search speed or efficiency. If the server has a lot of directories and we are sure about that the file / directory that we are searching is not in some directories, we can directly exclude those to improve the performance. The result will be faster as compared to the full search.
There are different ways to exclude a directory or multiple directories in FIND command. Here I’m listing some methods!
To explain this, I created the following directories and files:
- “cry“, “bit” and “com” directories.
- ” findme “: The test file in all directories.
Lets see the output:
Method 1 : Using the option “-prune -o”
We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command.
See the example:
The directory “bit” will be excluded from the find search!
Method 2 : Using “! -path”
This is not much complicated compared to first method. See the example pasted below:
Method 3 : Simple 🙂
Yes, it’s very simple. We can ignore the location by using inverse grep “grep -v” option.
See the example:
Excluding multiples directories
Similar way we can exclude multiple directories also. See the sample outputs:
Источник
Exclude list of files from find
If I have a list of filenames in a text file that I want to exclude when I run find , how can I do that? For example, I want to do something like:
and get all the .gz files in /dir except for the files listed in skip_files. But find has no -exclude_from flag. How can I skip all the files in skip_files ?
6 Answers 6
I don’t think find has an option like this, you could build a command using printf and your exclude list:
Which is the same as doing:
Alternatively you can pipe from find into grep :
This is what i usually do to remove some files from the result (In this case i looked for all text files but wasn’t interested in a bunch of valgrind memcheck reports we have here and there):
It seems to be working.
I think you can try like
The above command gives list of all files excluding files with .php, .jpg ang .png extension. This command works for me in putty.
Josh Jolly’s grep solution works, but has O(N**2) complexity, making it too slow for long lists. If the lists are sorted first (O(N*log(N)) complexity), you can use comm , which has O(N) complexity:
man your computer’s comm for details.
This solution will go through all files (not exactly excluding from the find command), but will produce an output skipping files from a list of exclusions. I found that useful while running a time-consuming command ( file /dir -exec md5sum <> \; ).
- You can create a shell script to handle the skipping logic and run commands on the files found (make it executable with chmod , replace echo with other commands):
- find . — Start find from current working directory (recursively by default)
- -type f — Specify to find that you only want files in the results
- -name «*_peaks.bed» — Look for files with the name ending in _peaks.bed
- ! -path «./tmp/*» — Exclude all results whose path starts with ./tmp/
- ! -path «./scripts/*» — Also exclude all results whose path starts with ./scripts/
- \( \) — groups operation (you can use -path «./tmp» -prune -o -path «./scripts» -prune -o , but it is more verbose).
- -path «./script» -prune — if -path returns true and is a directory, return true for that directory and do not descend into it.
- -path «./script» ! -prune — it evaluates as (-path «./script») AND (! -prune) . It revert the «always true» of prune to always false. It avoids printing «./script» as a match.
- -path «./script» -prune -false — since -prune always returns true, you can follow it with -false to do the same than ! .
- -o — OR operator. If no operator is specified between two expressions, it defaults to AND operator.
Create a file with the list of files to skip named files_to_skip.txt (on the dir you are running from).
Then use find using it:
Not the answer you’re looking for? Browse other questions tagged linux shell find or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Use find command but exclude files in two directories
I want to find files that end with _peaks.bed , but exclude files in the tmp and scripts folders.
My command is like this:
But it didn’t work. The files in tmp and script folder will still be displayed.
Does anyone have ideas about this?
6 Answers 6
Here’s how you can specify that with find :
Explanation:
Testing the Solution:
You were pretty close, the -name option only considers the basename, where as -path considers the entire path =)
Here is one way you could do it.
The order is important. It evaluates from left to right. Always begin with the path exclusion.
Explanation
Do not use -not (or ! ) to exclude whole directory. Use -prune . As explained in the manual:
and in the GNU find manual:
Indeed, if you use -not -path «./pathname» , find will evaluate the expression for each node under «./pathname» .
find expressions are just condition evaluation.
Hence, \( -path «./tmp» -o -path «./scripts» \) -prune -o -name «*_peaks.bed» -print is expanded to:
The print is important here because without it is expanded to:
-print is added by find — that is why most of the time, you do not need to add it in you expression. And since -prune returns true, it will print «./script» and «./tmp».
It is not necessary in the others because we switched -prune to always return false.
Hint: You can use find -D opt expr 2>&1 1>/dev/null to see how it is optimized and expanded,
find -D search expr 2>&1 1>/dev/null to see which path is checked.
Источник
Find command Exclude or Ignore Files (e.g. Ignore All Hidden .dot Files )
Find command exclude or ignore files syntax
The syntax is as follows:
Examples: find command and logical operators
Find any file whose name ends with either ‘c’ or ‘asm’, enter:
$ find . -type f \( -iname «*.c» -or -iname «*.asm» \)
In this example, find all *.conf and (.txt) text files in the /etc/ directory:
$ find . -type f \( -name «*.conf» -or -name «*.txt» \) -print
Fig.01: Linux find command exclude files command
Understanding find command operators
Operators build a complex expression from tests and actions. The operators are, in order of decreasing precedence:
( expr ) | Force precedence. True if expr is true |
expr -not expr ! expr | True if expr is false. In some shells, it is necessary to protect the ‘!’ from shell interpretation by quoting it. |
expr1 -and expr2 | expr2 is not evaluated if expr1 is false. |
expr1 -or expr2 | expr2 is not evaluated if expr1 is true. |
How do I ignore hidden .dot files while searching for files?
Find *.txt file but ignore hidden .txt file such as .vimrc or .data.txt file:
$ find . -type f \( -iname «*.txt» ! -iname «.*» \)
Find all .dot files but ignore .htaccess file:
$ find . -type f \( -iname «.*» ! -iname «.htaccess» \)
Say hello to -path option
This option return true if the pathname being examined matches pattern. For example, find all *.txt files in the current directory but exclude ./Movies/, ./Downloads/, and ./Music/ folders:
Источник
How do I make «find» exclude the folder it searches in?
I’m running the following command:
I would like to delete all the folders under the processing folder (the processing folder should never be deleted).
The command is deleting the processing folder as well. How do I limit the script to delete only the folders under that folder?
3 Answers 3
The easiest way would be to just add -mindepth 1 , which will skip the first depth hierarchy and thus leave out your parent directory.
Also, you don’t need an extra -exec call to rm , you can just delete the folders directly if they’re empty.
If they’re not empty:
If you’re lazy you can also have a wildcard expanded. Since * doesn’t include the current directory by default (unless dotglob is set), you could also do:
However, this would also not include hidden folders, again due to the dotglob option.
The problem is that find returns the current directory (.) along with the other directories, so it deletes the processing folder as well as the subdirectories. A quick way to get around that would be to append the option
which stops find from outputting the current directory, and in turn stops it from being deleted.
That would work if you were running the command within the processing directory, so to allow for the fact that you are using an absolute path:
Источник