- How can I recursively search for directory names with a particular string where the string is only part of the directory name
- 4 Answers 4
- How to Search and Find Files Recursively in Linux
- Finding Files Recursively in Linux
- Conclusion
- About the author
- David Adams
- How to Recursively Search Directory Names in Linux
- Searching Directories
- Searching With Regex
- Using grep With find
How can I recursively search for directory names with a particular string where the string is only part of the directory name
How can I recursively search for directory names with a particular string where the string is only part of the directory name? For example: the directory name is «8.0.3-99966_en», but I want to recursively search for directories with the string «99966».
4 Answers 4
You can use the find command:
find YOUR_STARTING_DIRECTORY -type d -name "*99966*" -print
find ~ -type d -name "*99966*" -print
should find all directories ( -type d ) starting from your home directory ( ~ )that have their names containing the string «99966» ( -name «*99966*» ) and output them ( -print ).
How can I exclude a certain directory from the search? I need to search / but I get tons of /proc results which I do not care about.
@Kuzuch (after a while!): you can use negative grep, piping the sinf search into a commend like: find
To avoid all of the «Permission denied» results, you can use:
find / -type d -name "*99966*" -print 2>/dev/null
See this article on null device and this one on standard streams for more info.
You can pipe the output to grep to have it highlight the directory name
Something like
find / -type d | grep "directory name"
The / indicates to search the whole computer
An easy way to do this is to use find | egrep string . If there are too many hits, then use the -type d flag for find. Run the command at the start of the directory tree you want to search, or you will have to supply the directory as an argument to find as well.
Another way to do this is to use ls -laR | egrep ^d .
And the locate command also comes in handy: locate string
How to Search and Find Files Recursively in Linux
This brief tutorial explains how to search and find the files recursively in the Linux operating systems.
After reading this article, you will be able to find any file recursively using the different techniques including a single file search, multiple files search, find files by permissions, and more. This document is optimized for both new and experienced Linux users. All methods are valid for every Linux distribution.
All examples in this tutorial contain screenshots to make it easy for any Linux user to understand and reproduce them.
Finding Files Recursively in Linux
The find command does not need flags to search the files recursively in the current directory. You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories.
The syntax is simple, as shown in the following:
If you want to find the 27.jpg file within your home directory and subdirectories, run the following command as shown in the following screenshot:
As you can see, the file was found in the /home/linuxhint/Downloads/recur subdirectory.
An alternative to the previous command is the tree command shown in the following example where you search the same file named 27.jpg within the current directory:
As you can see in the previous figure, the format is pretty different. It seems to be more user friendly or nicer as long as you don’t need to copy the full path to paste it.
The following screenshot shows how to use the find command to recursively search more than a file.
The syntax to search multiple files recursively is the following:
Note that there is a –o flag before the second file name. You can add more than one file by adding more –o –name flags. For example, to find 4 files with the same command, use the following syntax:
In the practical example described in the following image, use this command to find a file named 27.jpg and a file whose name begins with “DIAGRAM” but without specifying its extension. Instead, use a wildcard (*) to find any file named DIAGRAM independently of its type.
As you can see in the previous image, both files were found recursively.
The next example describes how to find the files by extension using the find command. In the following figure, you can see how to recursively find all the .jpg files using the wildcard again. The syntax is pretty simple:
Thus, to find all the .jpg files recursively, run the following command:
As shown in the previous image, all the jpg files including their path are listed successfully. You can replace the .jpg extension for any extension that you want to search like .png, .txt, .c and more.
Now, let’s assume that you don’t want to find a file but a directory recursively. All you need to do is to use the same command that was shown in the first example of this tutorial then add the -type d option. The syntax as follows:
In the following practical example, use the previous syntax to find the recur directory.
As you see in the previous figure, the directory named “recur” was found successfully.
You also can find the files by size using the following syntax where is the main directory containing the subdirectories and the is the size of the files that you can list with their full path.
The following example describes how to find the 10 MB size files. You can replace the M defining units in MB with c for bytes, w for two two byte words, k for kibytes and G for gibibytes (note units are case sensitive).
To find the 10 mebibytes files, execute the following command:
All 10M files were properly listed with their paths.
The syntax to find the files based on their permissions is shown in the following:
Let’s assume that you want to identify and list the files with read, write, and executing permissions (777). The command to run is the following:
The last example of this tutorial shows how to find and list the files and directories by size.
As shown, the files are listed by size with proper units. The 0 size directories and files are empty.
Conclusion
Linux versatility and flexibility allows to find the files (and other functions) recursively in many ways. They can easily be executed by all the Linux users independently of his knowledge level, from the new users to the system administrators. All techniques previously described are valid for all the Linux distributions and even to some Unix systems. According to their man pages, some flags may vary in some distributions, but most of them are universal. In case your Linux distribution does not match any of the previously explained commands, you can read the man page. It is highly recommended to the readers to practice the examples to incorporate this knowledge.
Thank you very much for reading this Linux tutorial. Keep following us for more Linux professional tips.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
How to Recursively Search Directory Names in Linux
Anthony Heddings
Anthony Heddings
Writer
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon’s AWS platform. He’s written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read more.
Everything in Linux is stored in directories, and when writing bash scripts, it’s often useful to search for directories by name. Luckily, you can use the find command to recursively search directory names and display matches.
Searching Directories
The find command is used to search through directories in Linux. By default, it’s fully recursive, so it will search through all sub-directories to find matches. If you use the -type d flag, find will operate in “directory mode,” and only search for directories, not matching any files. You can use it alongside -name to search for directories by name:
This command starts in the current directory but can also search in other directories like ~ .
The problem with using -name is it will only match direct names, meaning it will fail unless it matches the entire directory name. You can use wildcards to solve this though, and putting a wildcard before and after the search string will match the substring anywhere in the directory name. If you’re including filenames too, you can use wildcards to match files ending in a particular extension
However, this will only match the directory’s name, and will still ignore the parent directory. If you’d like to match using the entire file path, you’ll need to use the Regex option covered below.
find will print out a list of every directory that matches, but you’ll want to be careful to make sure you’re consistent in using either absolute or relative paths, because it will affect the final response. If you use a relative path, like the period for “current directory,” the responses will be relative. But if you specify the path directly, even if it’s the current directory, the path will be absolute, starting at root.
find also does more than just text searching—it can be used to match files based on timestamps, file sizes, and other Linux identifiers. It can also be used with -exec to run commands on each file or directory. RELATED: How to Use the find Command in Linux
Searching With Regex
You can also use more advanced filtering with find , using it with regular expressions (Regex) to find matches for complex search queries. One major benefit of using Regex is that it will match the entire directory, including the base directories leading up to.
You can use Regex with -regex in place of -name . It also helps to turn on sed -compatible Regex using -regextype sed .
find . -type d -regextype sed -regex “.*one\/.*” In this example, the regex starts with .*one to match all directories ending in “one.” The period and wildcard will match any substring leading up to this. Then the forward slash is escaped with \/ to match the end of the directory, and then another wildcard to match any directory name.
Overall, this will match any directory whose parent ends with “one,” anywhere it is, even in subdirectories. Regex is powerful, and you’ll want to be careful that yours matches exactly what you want it to do—no more, no less.
Using grep With find
Since find can also output a raw list of directories, it can be piped to other commands for processing. For example, grep is used as a text search utility, and is quick to use on the command line for simple search and highlighting.
grep is also a fully-fledged search utility on its own and can be used with tools like regular expressions to enhance the searching. You can read our guide to using it to learn more.
Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon’s AWS platform. He’s written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times.
Read Full Bio »