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 those
find Command to recursively search directory names and display matches.
find Command is used to search directories in Linux. By default, it’s fully recursive, so all subdirectories are searched for a match.
If you use the
-type d Flag,
find operates in “directory mode” and only searches for directories, not for matching files. You can use it in parallel
-name To search directories by name:
find . -type d -name "search"
This command starts in the current directory, but can also search in other directories, e.g
The problem with usage
-name It will only match direct names, which means it will fail if it doesn’t match the entire directory name. However, you can use wildcards to solve this, and if you put a wildcard before and after the search string, the substring will match anywhere in the directory name. If you also include filenames, you can use wildcards to match files ending with a specific extension
find . -type d -name "*search*"
However, this only matches the name of the directory and still ignores the parent directory. If you want to use the entire file path, you must use the regex option described below.
find prints a list of all matching directories, but you should be careful to consistently use either absolute or relative paths, as this will affect the final answer. When you use a relative path, like the dot for “current directory,” the answers are relative. However, if you specify the path directly, even if it is the current directory, the path is absolute and starts at root.
find also more than just a text search – 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 any file or directory.
TIED TOGETHER: How to use the Find command on Linux
Search with regex
You can also use advanced filtering with
findby using it with regular expressions (regex) to find matches for complex search queries.
A major benefit of using regex is that it will match the entire directory, including the base directories that carry it.
You can use regex with
-regex instead of
-name. It also helps with switching on
sed-Use compatible regex
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 match any preceding substring. Then the slash is masked with
/ for the end of the directory and then another wildcard for any directory name.
Overall, this will match any directory whose parent ends in “one” wherever it is, even in subdirectories. Regex is powerful, and you should make sure that yours is exactly what you want—nothing more, nothing less.
Using grep with find
find can also output a raw list of directories, it can be passed to other commands for processing. For example,
grep used as a text search utility and can be used quickly on the command line for basic searching and highlighting.
find . -type d | grep foo
grep is also a full-featured search utility and can be used with tools such as regular expressions to enhance searches. You can read our usage guide to learn more.
TIED TOGETHER: How to use the grep command on Linux
This article was previously published on Source link