How to Recursively Search Directory Names in Linux


Bash Shell

Everything in Linux is saved in directories, and when composing bash scripts, it’s typically beneficial to look for for directories by name. Fortunately, you can use the find command to recursively look for directory names and display matches.

Exploring Directories

The locate command is applied to research by way of directories in Linux. By default, it is completely recursive, so it will research by way of all sub-directories to come across matches.

If you use the -sort d flag, discover will run in “directory manner,” and only search for directories, not matching any information. You can use it together with -title to research for directories by identify:

obtain . -variety d -name "lookup"

This command starts off in the current listing but can also look for in other directories like ~.

The difficulty with employing -name is it will only match immediate names, this means it will are unsuccessful unless it matches the overall listing title. You can use wildcards to fix this while, and putting a wildcard ahead of and after the lookup string will match the substring any place in the listing name. If you are including filenames also, you can use wildcards to match data files ending in a individual extension

come across . -sort d -name "*search*"

Nevertheless, this will only match the directory’s title, and will continue to overlook the father or mother directory. If you’d like to match utilizing the complete file path, you’ll will need to use the Regex selection included down below.

find will print out a checklist of each listing that matches, but you’ll want to be very careful to make confident you are reliable in employing both complete or relative paths, mainly because it will have an effect on the remaining reaction. If you use a relative route, like the time period for “current directory,” the responses will be relative. But if you specify the path directly, even if it is the present directory, the path will be absolute, commencing at root.

uncover also does more than just text searching—it can be applied to match documents centered on timestamps, file measurements, and other Linux identifiers. It can also be utilized with -exec to run instructions on every single file or directory.

Similar: How to Use the obtain Command in Linux

Hunting With Regex

You can also use extra highly developed filtering with uncover, applying it with typical expressions (Regex) to come across matches for intricate search queries.

A person major benefit of making use of Regex is that it will match the complete listing, like the foundation directories major up to.

You can use Regex with -regex in area of -name. It also can help to change on sed-appropriate Regex applying -regextype sed.

come across . -style d -regextype sed -regex “.*1/.*”

In this illustration, the regex starts with .*a single to match all directories ending in “one.” The period of time and wildcard will match any substring leading up to this. Then the ahead slash is escaped with / to match the end of the directory, and then one more wildcard to match any directory identify.

In general, this will match any listing whose guardian ends with “one,” everywhere it is, even in subdirectories. Regex is powerful, and you will want to be mindful that yours matches exactly what you want it to do—no more, no much less.

Applying grep With find

Due to the fact come across can also output a uncooked record of directories, it can be piped to other instructions for processing. For instance, grep is made use of as a textual content lookup utility, and is speedy to use on the command line for straightforward lookup and highlighting.

discover . -variety d | grep foo

grep is also a completely-fledged search utility on its personal and can be utilised with resources like normal expressions to enhance the seeking. You can examine our manual to applying it to understand much more.

Connected: How to Use the grep Command on Linux





Resource hyperlink