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...

Spread the love

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 --basename or -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'
Find Directory Using locate Command

Find Directory Using locate Command

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"
Search a Directory Using find Command

Search a Directory Using find Command

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 -ls:

$ sudo find . -type d -name "pkg" -ls
Find and List Directory

Find and List Directory

Next, the option -iname will enable a case insensitive search:

$ sudo find . -type d -iname "pkg" $ sudo find . -type d -iname "PKG" 
Find Directory with Case Sensitive

Find Directory with Case Sensitive

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.

Facebook Comments

More Stuff

Fedora 22 Released – See What’s New in Workstation, Server and Clo... The Fedora project has announced the release of one of the most awaited Linux Distribution (of the Year 2015) Fedora 22 on May 26, 2015. Fedora is a R...
How to Compress and Decompress a .bz2 File in Linux To compress a file(s), is to significantly decrease the size of the file(s) by encoding data in the file(s) using less bits, and it is normally a usef...
How to Repair and Defragment Linux System Partitions and Directori... People who use Linux often think that it doesn’t require defragmentation. This is a common misunderstanding across Linux users. Actually, the Linux op...
Deal: Get The Interactive Coding Bootcamp to Build Real-World Apps... In this digital age, the world is actually powered by code, and only those aspiring to control the world by building a portfolio of current, real-worl...
Spread the love

Posted by News Monkey