Ways to Use ‘find’ Command to Search Directories More Efficiently
This tutorial will take you through the different ways of finding a directory in Linux. As you may already know, in Linux everything is a file including directories. And one of the common things a Linux user will do within the command line is searching for a file or a directory.
There are several different means and utilities used for searching for files on the command line such as find, locate and which. However, the last utility (which) is only used for locating a command.
For the scope of this tutorial, we will mainly focus on the find utility, which searches files on a live Linux filesystem and is more efficient and reliable as compared to locate.
The downside of locate is that it reads one or more databases created by updatedb, it does not search through a live filesystem. In addition, it does not as well offer flexibility regarding where to search from (starting point).
Below is the syntax for running locate command:
# locate [option] [search-pattern]
To demonstrate the disadvantage of locate, let us assume we are searching for a directory named
pkg in the current working directory.
Note: In the command below, the option
-b tells locate to only match the file (directory) basename (which is exactly pkg) but not the path (/path/to/pkg). Where
\ is a globbing character, it disables the implicit replacement of pkg by *pkg*.
$ locate --basename '\pkg'
As you can see from the command output above, locate will search beginning from the root (/) directory, that is why other directories with the same name are matched.
Therefore, to deal with this issue, use find by following the simplified syntax below:
$ find starting-point options [expression]
Let us look at a few examples.
To search for the same directory
(pkg) above, within the current working directory, run the following command, where the
-name flag reads the expression which in this case is the directory basename.
$ find . -name "pkg"
If you encounter “Permission denied” errors, use sudo command like so:
$ sudo find . -name "pkg"
You can prevent find from searching for other file types except directories by using
-type flag to specify the type of file (in the command below
d means directory) as follows:
$ sudo find . -type d -name "pkg"
Furthermore, if you wish to list the directory in a long listing format, employ the action switch
$ sudo find . -type d -name "pkg" -ls
Next, the option
-iname will enable a case insensitive search:
$ sudo find . -type d -iname "pkg" $ sudo find . -type d -iname "PKG"
To find more interesting and advanced usage information, read the man pages of find and locate.
$ man find $ man locate
As a last remark, the find command is more reliable and efficient for searching files ( or directories) in a Linux system when weighed against the locate command.
In the same way as before, do not forget to send us your feedback or questions via the comment section below. Lastly, always remain connected to Tecmint.