How to Recursively Search Directory Names in Linux


Bash Shell

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:

find . -type d -name "search"

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

find . -type d -name "*search*"

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.

find . -type d | grep foo

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.

RELATED: How to Use the grep Command on Linux





Source link

Leave a Reply

Your email address will not be published.