How to Recursively Search Directory Names in Linux
All the things in Linux is stored in directories, and when producing bash scripts, it is often helpful to search for directories by identify. Fortunately, you can use the
locate command to recursively research directory names and display screen matches.
discover command is employed to search by directories in Linux. By default, it’s entirely recursive, so it will look for via all sub-directories to uncover matches.
If you use the
-kind d flag,
obtain will run in “directory method,” and only research for directories, not matching any documents. You can use it along with
-identify to search for directories by title:
obtain . -kind d -identify "search"
This command begins in the current directory but can also lookup in other directories like
The challenge with making use of
-identify is it will only match immediate names, which means it will fail except if it matches the overall directory title. You can use wildcards to resolve this although, and putting a wildcard in advance of and just after the look for string will match the substring everywhere in the directory identify. If you are such as filenames much too, you can use wildcards to match files ending in a certain extension
come across . -kind d -title "*lookup*"
Nonetheless, this will only match the directory’s name, and will still disregard the mum or dad listing. If you’d like to match employing the whole file path, you will require to use the Regex possibility covered under.
come across will print out a checklist of just about every listing that matches, but you will want to be watchful to make absolutely sure you are constant in applying either complete or relative paths, simply because it will have an effect on the remaining response. If you use a relative path, like the period for “current directory,” the responses will be relative. But if you specify the route specifically, even if it is the current directory, the route will be absolute, starting off at root.
obtain also does far more than just text searching—it can be utilised to match data files based on timestamps, file dimensions, and other Linux identifiers. It can also be used with
-exec to run instructions on every single file or directory.
Relevant: How to Use the discover Command in Linux
Hunting With Regex
You can also use a lot more state-of-the-art filtering with
obtain, applying it with typical expressions (Regex) to uncover matches for advanced lookup queries.
One important gain of utilizing Regex is that it will match the whole directory, together with the foundation directories primary up to.
You can use Regex with
-regex in position of
-identify. It also aids to transform on
sed-compatible Regex employing
discover . -kind d -regextype sed -regex “.*a single/.*”
In this illustration, the regex starts with
.*just one to match all directories ending in “one.” The period of time and wildcard will match any substring main up to this. Then the ahead slash is escaped with
/ to match the end of the listing, and then a further wildcard to match any directory title.
Overall, this will match any listing whose mum or dad ends with “one,” everywhere it is, even in subdirectories. Regex is potent, and you are going to want to be thorough that yours matches precisely what you want it to do—no additional, no considerably less.
Working with grep With discover
Considering the fact that
uncover can also output a uncooked list of directories, it can be piped to other instructions for processing. For case in point,
grep is utilized as a text research utility, and is speedy to use on the command line for uncomplicated lookup and highlighting.
find . -variety d | grep foo
grep is also a totally-fledged search utility on its personal and can be used with tools like standard expressions to enhance the exploring. You can go through our tutorial to using it to understand more.
Associated: How to Use the grep Command on Linux