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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

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.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

6 Responses

  1. Shashi says:

    Nice post.
    Really this helps more.

  2. Bhargav Talla says:

    Thank You very much for sharing.
    Every day I am learning new things and getting knowledge because of Tecmint.

  3. Prashant Thorat says:

    Nice Post, really useful to know these command-line tricks..

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.