To exclude a directory in the find command, use the -prune option. For example, to exclude the directory “dir1” from the search, use the following command:
find . -name “*” -prune -o -type d -name “dir1” -prune
The find command is a very commonly used command in Linux/Unix systems. Basically, it is used to search and find files inside directories and subdirectories. When you are working in Linux/Unix system. And at that time you have to search any file, directory, or sub-directory anytime and exclude any file, directory or sub-directory from these searches.
In this tutorial, you will learn how to exclude files, directories, and sub-directories from a search using the ‘find’ command.
Before you dive into excluding directories from the “find” command, let’s take a quick look at the basic syntax of the command:
find [path] [expression]
Here, the “path” is the starting directory for the search, and the “expression” is a set of rules or conditions that define the search criteria.
How to Exclude Files, Directory, and Subdirectory in Find Command
By using these methods, you can exclude files, directories, and sub-directories from search in find command:
- Excluding a Directory From the Search
- Excluding Multiple Directories From the Search
- Excluding Files from the Search
- Excluding Multiple Files from the Search
Excluding a Directory From the Search
To exclude a directory from the “find” command, you can use the “-prune” option. This option tells “find” to skip the specified directory and not to descend into it. Here is the basic syntax for excluding a directory:
find [path] -name [directory_to_exclude] -prune -o [expression]
In this syntax, the “-name” option specifies the name of the directory to exclude, and the “-prune” option tells “find” to skip this directory. The “-o” option specifies the alternative expression to search for. You can use any valid expression after the “-o” option.
For example, suppose you want to search for all files in the “/home/user” directory but exclude the “/home/user/Apps” directory. Here is how you can do it:
find /home/user -name Apps -prune -o -type f
The above-given command will list all files in the “/home/user” directory and its subdirectories except for the “/home/user/Apps” directory.
Let’s use another example to search for all files in the current directory and its subdirectories but exclude any directory named “my_modules”. Here is the command:
find . -name node_modules -prune -o -type f
The above-given command will list all files in the current directory and its subdirectories except for the “my_modules” directory.
Excluding a Multiple Directories From the Search
If you want to exclude multiple directories from the search. You can do this by using multiple “-name” and “-prune” options followed by the “-o” option. Here is an example:
find /var/log -name dir1 -prune -o -name dir2 -prune -o -type f
The above given command will search for all files in the “/var/log” directory and its subdirectories but exclude the “dir1” and “dir2” directories.
Excluding Files from the Search
If you want to search for all files in the current directory and its subdirectories but exclude any file with the extension “.txt”. Here is how you can do it:
find . -not -name '*.txt' -type f
The above-given command will list all files in the current directory and its subdirectories except for those with the “.txt” extension.
Excluding Multiple Files from the Search
If you want to search for all files in the current directory and its subdirectories but exclude multiple files with the extension “.txt”. Here is how you can do it:
find . -type f \( -name '*.txt' -or -name '*.log' \) -not -name '*backup*'
The above-given command will list all files in the current directory and its subdirectories with the extensions “.txt” or “.log” but exclude any file with the word “backup” in its name.
Here are some numerous frequently asked questions about excluding files and directories in the “find” command in Linux:
Q: Can I exclude hidden directories from a “find” search?
A: Yes, you can exclude hidden directories (directories whose names start with a dot) from a “find” search by using the “-not -path” option with the “-name” option. For example, to exclude all hidden directories, use the following command:
find . -type d -not -path '*/.*'
Q: Can I use regular expressions to exclude directories in a “find” search?
A: Yes, you can use regular expressions to exclude directories in a “find” search. Use the “-regex” option with the regular expression pattern to exclude directories that match the pattern. For example, to exclude all directories that start with the word “test”, use the following command:
find . -type d -not -regex '.*\/test.*'
Q: How do I include both files and directories in a “find” search, excluding a certain directory?
A: To include both files and directories in a “find” search, excluding a certain directory, use the “-not -path” option followed by the directory path, and use the “-o -type f” option to include files in the search. For example, to search for all files and directories in the current directory and its subdirectories, except for those in the “logs” directory, use the following command:
find . -not -path "./logs/*" -o -type f
Q: Can I exclude directories based on their size?
A: Yes, you can exclude directories based on their size using the “-not -path” option with the “-size” option. For example, to exclude all directories that are larger than 1GB, use the following command:
find . -type d -not -path "./logs/*" -size +1G
Q: Can I exclude directories based on their modification time?
A: Yes, you can exclude directories based on their modification time using the “-not -path” option with the “-mtime” option. For example, to exclude all directories that have been modified in the last 24 hours, use the following command:
find . -type d -not -path "./logs/*" -mtime -1
Q: Can I use regular expressions to exclude files in the “find” command?
A: Yes, you can use regular expressions to exclude files in the “find” command by using the “-regex” option followed by the regular expression pattern. For example, you can exclude files with names that start with “test” using the following command:
find . -type f -not -regex '.*/test.*'
Q: Can I exclude files based on their size using the “find” command?
A: Yes, you can exclude files based on their size using the “-size” option followed by the file size and a unit. For example, you can exclude files that are larger than 1MB using the following command:
find . -type f -size +1M -not -name '*.txt'
Q: Can I exclude files based on their ownership in the “find” command?
A: Yes, you can exclude files based on their ownership using the “-user” and “-group” options followed by the username or group name. For example, to exclude files owned by the user “john”, use the following command:
find . -type f -user john -not -name '*.txt'
Conclusion
Excluding a directory from the “find” command can be useful when you want to narrow down your search results or avoid certain directories. By using the “-prune” option with the “-name” option, you can tell “find” to skip a directory and not descend into it. And exclude some files that you don’t want to be included in the search. By using the “-not” option together with the “-name” option, you can tell “find” to exclude files with a certain name or pattern.