Linux provides different tools or command in order to search and find directories. The find
command is the most popular and useful command to find directories in Linux. The find command can be used to search and find directories according to the path, name, name pattern, and related parameters. In this tutorial, we examine how to find the directory in different ways in Linux. We will also cover the locate
command to make a faster and cached search in the directory database to find directories.
Find Directory In Whole System
The following find command can be used to search the directory named db
in the whole Linux file system. The -type d
is used to search only directories, not files. The name of the directory is provided with the -name
option.
$ find / -type d -name "db"
Find Directory Using Root Privileges
A regular user does not have access to all files or directories in Linux and searching them also requires extra privileges like root privileges. If the root privileges are not provided during the search of the whole file system of Linux the Permission denied
error is printed a lot of time which fills the output and makes it hard to see real search results.
The sudo
command can be used to search the whole Linux file system with root privileges which prevent the “Permission denied” errors.
$ sudo find / -type d -name "db"
Find Directory In Specified Path
We can use the find command in order to search a specified directory at the specified path. The path is specified after the find command. In the following example, we search the directory in the “/home/ismail” path.
$ find /home/ismail -type d -name "db"
Find Directory with Case Insensitive Name
By default, the find command searches the provided directory name as case insensitive
. The -iname
parameter is used to provide the directory name instead of -name
parameter. The following search matches with a directory named “Db”,”db”,”DB”.
$ find /home/ismail -type d -iname "db"
Hide Errors During Directory Search
While using the find command a lot of paths are searched for the specified directory name. This may create different errors like access, and permission errors which are directly printed to the terminal. This makes it hard to read the output. We can redirect these errors using stderr or Standard Error
output using the 2>
operator. In the following example, we redirect errors to the null. These errors generally occur during the search of the whole Linux file system.
$ find / -type d -name "db" 2> /dev/null