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

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

6 Comments

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.