- Exclude a sub-directory using find
- 6 Answers 6
- Exclude Directories While Using Find Command
- Method 1: Using the prune option
- Exclude multiple directories
- Method 2: Using the not operator
- Method 3: Using the ! operator
- Bonus tip: Exclude Subdirectories in find command
- Final Words
- How to exclude this / current / dot folder from find «type d»
- 5 Answers 5
- 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 a sub-directory using find
There is an incoming sub-folder in all of the folders inside Data directory. I want to get all files from all the folders and sub-folders except the def/incoming and 456/incoming dirs. I tried out with following command
find /home/feeds/data -type d \( -name 'def/incoming' -o -name '456/incoming' -o -name arkona \) -prune -o -name '*.*' -print
This is not good advice, but it will get you out of a lot of situations quick and dirty: pipe that to grep -v something to exclude whatever it is you don’t want
6 Answers 6
find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*"
Explanation:
- find /home/feeds/data : start finding recursively from specified path
- -type f : find files only
- -not -path «*def/incoming*» : don’t include anything with def/incoming as part of its path
- -not -path «*456/incoming*» : don’t include anything with 456/incoming as part of its path
@Ravi are you using bash shell? I just tested this on my terminal and it works for me. Try copy and pasting the solution instead if you made modifications to your script.
-path matches the whole string, so if you’re doing find . , then your -path strings need to be ./path/to/directory/*
FYI -not -path definitely will work in this example but find is still iterating into the directory structure and using cpu cycles to iterate over all those directories/files. to prevent find from iterating over those directories/files (maybe there are millions of files there) then you need to use -prune (the -prune option is difficult to use however).
Just for the sake of documentation: You might have to dig deeper as there are many search’n’skip constellations (like I had to). It might turn out that prune is your friend while -not -path won’t do what you expect.
So this is a valuable example of 15 find examples that exclude directories:
To link to the initial question, excluding finally worked for me like this:
find . -regex-type posix-extended -regex ".*def/incoming.*|.*456/incoming.*" -prune -o -print
Then, if you wish to find one file and still exclude pathes, just add | grep myFile.txt .
It may depend also on your find version. I see:
$ find -version GNU find version 4.2.27 Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX
Exclude Directories While Using Find Command
By default, find command searches in the specified directory and all its subdirectories. You can refine your search by excluding directories while using find command.
Find is an extremely powerful command for searching for anything you want on your Linux system.
By default, it searches in the specified directory and all its subdirectories.
You may not always want that. You can refine your search by excluding directories from the find command search.
In this tutorial, I’ll show various ways to exclude directories while using the find command.
Method 1: Using the prune option
First, let me bring light to how you’re about to use the find command with the prune option:
find [path] -path [directory to exclude] -prune -o -print
For example, I’ve made a directory named prune which contains the following files and directories:
So how about excluding music directory while performing a search?
find /home/sagar/prune -path /home/sagar/prune/music -prune -o -print
As you can clearly see, it only went through the text and images directory as I excluded the 3rd directory.
Exclude multiple directories
Quite easy right? But what about when you want to exclude multiple directories? That can easily be done using the -o operator.
For example, I’ll be excluding music and text directory:
find . \( -path ./music -prune -o -path ./text -prune \) -o -print
In simple terms, you have to chain your directories with -prune and -o inside () as shown fashion to exclude multiple directories.
But find is not bound to only search for files but when paired with exec, can execute scipts, programs, or even commands over output:
Method 2: Using the not operator
Using the not operator is easy compared to what I explained above as syntax is quite simple to grasp:
find [path] -type f -not -path '*/directory to exclude/*'
For example, let’s exclude the music directory using the not operator:
find . -type f -not -path '*/music/*'
As you can clearly see, the search results do not include any files related to the music directory.
Method 3: Using the ! operator
Yet another easy way to exclude directories while searching is to use the ! operator.
The syntax is similar to what I explained above but a little short in length:
find /path/ -type f ! -path '*/directory to exclude/*'
Let’s say I want to exclude a directory named text so my command would be:
But that’s not it. Being one of the most extensive commands, find can also search for files based on modification time.
Bonus tip: Exclude Subdirectories in find command
Well, this is a bit different as this section is going to utilize the term called search depth .
This means I will be specifying how deeper the find utility will search. Here, deep means the layers of the file system. The general structure of the file system looks like this:
And you can adjust the depth of search by using the maxdepth and mindepth options.
So when you use the maxdepth, you’re giving limits to the find utility, or in simple terms, you’re saying «don’t go any further than this limit».
While mindepth is all about where to start the search and will look for every available file at every layer.
So let’s suppose I want to search for text files that are available at the first layer so I’ll be using maxdepth to control the search:
find . -maxdepth 1 -type f -name "*.txt"
But what if you want to search into a specific layer that sits in the middle? Well, that’s where the mindepth comes to play!
Let’s suppose I want to search for PNG images specifically inside the Images directory (2nd layer) so I will use maxdepth and mindepth to 2 forcing find to search for a specific directory.
find . -maxdepth 2 -mindepth 2 -type f -name "*.png"
Be creative with maxdepth and mindepth and you’ll be able to search for specific files in no time!!
Final Words
This was my take on how to exclude directories while searching files by various methods and if you still have any doubts, feel free to ask in the comments.
How to exclude this / current / dot folder from find «type d»
can be used to find all directories below some start point. But it returns the current directory ( . ) too, which may be undesired. How can it be excluded?
5 Answers 5
Not only the recursion depth of find can be controlled by the -maxdepth parameter, the depth can also be limited from “top” using the corresponding -mindepth parameter. So what one actually needs is:
works on GNU find, but unfortunately is a gnu extension to the POSIX 7 find, and even the LSB uses POSIX shell utilities (not the GNU extended ones)
For this particular case ( . ), golfs better than the mindepth solution (24 vs 26 chars), although this is probably slightly harder to type because of the ! .
To exclude other directories, this will golf less well and requires a variable for DRYness:
D="long_name" find "$D" ! -path "$D" -type d
My decision tree between ! and -mindepth :
- script? Use ! for portability.
- interactive session on GNU?
- exclude . ? Throw a coin.
- exclude long_name ? Use -mindepth .
if you need to exclude multiple paths just do find /path/ ! -path «/path/first» ! -path «/path/second» is this only way?
@VincentDeSmet do you want to exclude just those paths, or actually not recurse into them? If just the paths, you can use find / ! -regex ‘/\(a\|b\)/.*’ or more simply, pipe through grep. To not recurse, the above would be very inefficient and you should use -prune : stackoverflow.com/questions/1489277/…
my issue was as follows: I wanted to recursively delete everything within a directory except for 1 sub directory. I was using find with grep to exclude the directory but the parent directory was still there, causing everything to be deleted anyway.
@VincentDeSmet I don’t see a direct solution with find , you’d need to check for prefixes: stackoverflow.com/questions/17959317/… But a Bash for loop can handle it 🙂
@CiroSantilli烏坎事件2016六四事件法轮功 — my bad — it does NOT include the current directory. It does return other dot-prefixed directories — but after reading the OP, I see that your answer only tries to exclude «current dir»?
I use find ./* <. >when I don’t mind ignoring first-level dotfiles (the * glob doesn’t match these by default in bash — see the ‘dotglob’ option in the shopt builtin: https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html).
eclipse tmp # find . . ./screen ./screen/.testfile2 ./.X11-unix ./.ICE-unix ./tmux-0 ./tmux-0/default
eclipse tmp # find ./* ./screen ./screen/.testfile2 ./tmux-0 ./tmux-0/default
FYI. do not use this trick with -exec option. For example, if you try find dir/* -type d -exec rmdir <> \; , you will see errors.
You are mistaken, or perhaps misadvised. That command will work fine. If you are seeing errors, they will be coming from rmdir and are most likely telling you that the directories are not empty since find will do a depth-first search into the directories, showing the parents before their children.
Well, a simple workaround as well (the solution was not working for me on windows git bash)
It might not be very performant, but gets the job done, and it’s what we need sometimes.
[Edit] : As @AlexanderMills commented it will not show up hidden directories in the root location (eg ./.hidden ), but it will show hidden subdirectories (eg. ./folder/.hiddenSub ). [Tested with git bash on windows]
Exclude a directory or multiple directories while using find command
As a Linux server administrator or DevOps engineer we need to use find command quite frequently to find a lot of stuff from the server. We can use the find command to find files and directories based on different things; like based on file permission, ownership, size, access time etc. In this blog article we are discussing how we can exclude some directories while doing the find command.
If you have a lot directories, it takes time to do the find operation. This exclude will help you to reduce the execution time while doing the find command.
Is it possible to exclude a directory with find command? Exclude directories while doing running find command? I have a lot directories and how to exclude selected directories while performing the find command to save the find execution time.
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:
# find -iname findme ./bit/findme ./com/findme ./cry/findme
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.
# find ./ -path ./bit/* -prune -o -iname findme -print ./com/findme ./cry/findme
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:
# find -iname findme ! -path ./bit/* ./com/findme ./cry/findme
If you are interested to read some Kubernetes topics you can check this page https://www.crybit.com/category/devops/k8s/
Method 3 : Simple 🙂
Yes, it’s very simple. We can ignore the location by using inverse grep “grep -v” option.
This is not the recommended way. It do the grep after performing the find operation. So there is no advantage considering the find command execution time. It does the find first then exclude the specific string.
# find -iname findme|grep -v bit ./com/findme ./cry/findme
Excluding multiples directories
Similar way we can exclude multiple directories also. See the sample outputs:
# find -iname findme ! -path ./bit/* ! -path ./cry?* ./com/findme
# find -iname findme | egrep -v "bit|cry" ./com/findme
That’s it. Compose it your own ways!!