You may often need to locate specific directories for various purposes, such as finding configuration, identifying folders containing backup data, or searching for project-related directories. One of the most efficient ways to find a directory by name is using the 'find' command.
In this short article, I will demonstrate how to search folders by name in Linux using an example. Let's find all directories with the name "user" in the /etc/ directory:
sudo find /etc/ -type d -name "user"
Let's break down the command and understand how it works:
- sudo: In this case, the command includes sudo to grant administrative privileges.
- /etc/: This is the starting directory for the search, in this case, it's the /etc/ directory.
- -type d: This flag specifies that we are searching for directories (
d
stands for directory). - -name "user": This flag specifies that we are searching for items named "user". Note, that the search is case-sensitive, so it will only return exact matches.
Upon execution, this command will traverse the /etc/ directory and its subdirectories, searching for directories with the name "user" and then display the full path of each matching directory.
If you can't recall the exact name of a folder, you can use a wildcard pattern to find all directories whose names contain a specific word. For instance, use the following command will find directories whose names include "config":
sudo find /etc/ -type d -name "*config*"
For more information on searching directories or files, refer to this article.