How to Exclude Certain Paths With the find Command in Linux
Last Updated : 01 Dec, 2022
The find command in Linux is a very useful command line utility that helps us in finding files and directories. In our daily life, there are many tasks in which we require files that may be located deep inside the system, finding them manually would be a tedious task. Thus, the find command comes in handy.
We can improve the performance of this command by excluding certain files and directories so that it doesn't look into them.
Ways to exclude paths with the find command:
Method 1: The -prune flag
Syntax:
find <path_of_the_file> -prune
For the sake of this article, we will be creating sample files and directories which will be used for demonstration purposes.
mkdir dir1 dir2 dir3 touch dir1/cat dir1/dog dir1/ant touch dir2/apple dir2/cherry dir2/grape touch dir3/car dir3/bus dir3/bike
Let us look at the tree of this directory system: (install the tree command by sudo apt install tree)
Now we will use the -prune option to exclude a certain path while searching:
find . -path ./dir3 -prune -o -print
This will enable the system to look for all the directories except the dir3 directory.
For simplicity we have put all three directories within a sample directory so that all the files in the system are not searched, otherwise, the list would be too long. The result from the output is clear that dir3 was excluded and all the remaining directories were printed.
Excluding multiple directories:
We can also exclude multiple paths if we want to :
find . \( -path ./dir1 -prune -o -path ./dir3 -prune \) -o -print
Both directories have been excluded from the search.
Method 2: The -not flag
The -not flag can also be used to exclude directories from the scope of search:
Syntax:
find . -type f -not -path '*/directory_name/*'
Let us exclude dir1 from the search scope:
find . -type f -not -path '*/dir1/*'
The not flag also excluded the dir1 directory, although its syntax is a bit complex it performs the same action.
Method 3: The "!" operator
The ! flag like the prune and not flags can be used to exclude the path:
Syntax:
find . -type f ! -path '*/directory_name/*'
Let us exclude the dir2 directory now:
find . -type f ! -path '*/dir2/*'
With the help of ! operator the second directory(dir2) was not searched.
So these were the three ways in which you can exclude certain paths from the scope of the find command.
Similar Reads
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
Difference between locate, which and find Command in Linux In Linux, there are multiple utilities available to locate files efficiently within a system. Three of the most commonly used commands for this purpose are:findlocatewhichEach command serves a similar function but differs in the way they search and the data they return. In this article, we'll explor
5 min read
locate command in Linux with Examples locate command in Linux is used to find the files by name. There are two most widely used file-searching utilities accessible to users called to find and locate. The locate utility works better and faster than the find command counterpart because instead of searching the file system when a file sear
6 min read
How to Get the Full Path of a File in Linux While dealing with files on Linux, especially shell scripts, one may require determining the full path of a file at times. Now, let's consider several methods of getting the full path of a file in Linux. In this article, we will be discussing several different solutions to a popular problem.Before w
3 min read
Linux - Installing locate Command to Find Files In this article, we will see how to install locate command to find files in Linux. locate is a command-line interface tool used for finding files by name in Linux systems. This works more efficiently than other commands. It uses one or more databases by updatedb. To check whether locate utility is a
2 min read