While managing the linux servers we need to use the find command for finding files and directories based on our use case. In this article we want to learn about how to exclude a directory or multiple directories while using find command.
Table Of Contents
Using the prune option in find command
In general, servers will have lot of directories if we want to perform search. To improve the speed and efficiency we can exclude the directories. Then will get the better results. Inorder to exclude certain directories from search, we can use the prune option. Lets looks the syntax of find command with prune option.
find path <excluding path> -prune
-prune: The primary shell evaluates as true. -O: OR operator. If no operator is specified between two expressions, it defaults to AND operator.
Let’s see some examples,
Example 1: Using the prune -o option – Exclude the single directory
Lets have an example which will exclude the test2 folder which means it will perform search excluding that directory.
find . -path ./test2 -prune -o -print
It will return remaining files or directories excluding that directory.
Frequently Asked:
Output
./test.json ./test1/samplefile.sql ./dbfiles/master/Vtable.sql
We have to use prune option along with “-o print”, otherwise the find command will only display the files that contains the name test2. Lets see how prune option works without “-o print”
find . -path ./test2 -prune
It will return the folders matching test2.
Output
./test2
Exclude multiple Directories
find . \( -path ./test2 -prune -false -o -path ./test1 -prune -false \) -o -print
Output
./test.json ./dbfiles/master/Vtable.sql
- We can also simulate ls command using prune option. Lets looks the syntax for displaying all files in current directory, excluding the current directory
find . ! -name . -prune
Output
./test.json ./dbfiles/master/Vtable.sql
The “! -name .” means any file other than the current directory. So prune will exclude all directories other than current directory including its sub-directories.
- We can use below option as well to get the output same as above command.
find ./* -prune
Output
./test.json ./dbfiles/master/Vtable.sql
Here, “.*/” means all files and directories under current directory. So prune will prevent the find command to traverse through sub-directories. But this command is not recommended if you have multiple folders in the current directory.
Using wholename along with prune option
find . -wholename "./test2" -prune -o -print
Output
./test.json ./test1/samplefile.sql ./dbfiles/master/Vtable.sql
When we use prune option to exclude a particular directory, most of the times we face an issue like there will be multiple folders with the same name in any of the sub directories. Then we can exclude only a particular directory using the above option.
Using exec and prune to exclude a directory
find . -name test2 -exec test '{}' = "./test2" \; -prune -o -print
Output
./test.json ./test1/samplefile.sql ./dbfiles/master/Vtable.sql
This command will not exclude any sub-directory with name as test2. This option can be used as an alternative to “-wholename” option
Using exec and prune to exclude a directory
find . -name test2 -prune -print -o -print
Output
./test.json ./test1/samplefile.sql ./dbfiles/master/Vtable.sql
This command will not exclude any sub-directory with name as test2. This option can be used as an alternative to “-wholename” option
Using inum and prune to exclude a directory
find . -inum 1584545 -prune -o -print
Output
./test.json ./test1/samplefile.sql ./dbfiles/master/Vtable.sql
If we know the Inode number of the directory that we want to exclude then we can use the -inum option along with prune to exclude that directory.
Using “! -path” option
We can exclude the directories with ! operator.
Exclude the single directory
find . -type f ! -path './test2/*'
Output
./test.json ./test1/samplefile.sql ./dbfiles/master/Vtable.sql
Exclude multiple Directories
find . -type f ! -path './test2/*' ! -path './test1/*'
Output
./test.json ./dbfiles/master/Vtable.sql
Using “not” option
We can exclude the directories with not operator.
Exclude the single directory
find . -type f -not -path './test2/*'
Output
./test.json ./test1/samplefile.sql ./dbfiles/master/Vtable.sql
Exclude multiple Directories
find . -type f -not -path './test2/*' -not -path './test1/*'
Output
./test.json ./dbfiles/master/Vtable.sql
Summary
We learned different ways to exclude a directory when using the “find” command in Linux. Thanks.