How to Find a File in Ubuntu Linux Terminal Using the locate Command

Navigating the Linux file system through the command line can sometimes feel like searching for a needle in a haystack. While the find command is incredibly powerful for searching specific directories in real-time, it can be slow because it physically scans the hard drive. If you need to find a file instantly, the locate command is often the better tool. Instead of searching your hard drive, locate queries a pre-built database of all the files on your system, returning results in milliseconds. This makes it the fastest way to find a missing document or configuration file in Ubuntu.

Step 1: Install the locate Utility (If Necessary)

Unlike find, the locate command is not always installed by default on newer versions of Ubuntu.

  1. Open your terminal (Ctrl + Alt + T).
  2. Attempt to run a simple locate command: locate --version
  3. If the terminal returns “Command ‘locate’ not found,” you need to install it. Type the following command and press Enter:
    sudo apt update && sudo apt install plocate
  4. Enter your password when prompted. (Note: plocate is the modern, faster replacement for the older mlocate package, but it still uses the same locate command).

Step 2: Update the Database

Because locate relies on a database to find files, it will not be able to find a file you just created five minutes ago. The database is usually scheduled to update once a day automatically. If you just installed locate, or if you need to find a brand-new file, you must force the database to update.

  1. In your terminal, type the following command:
    sudo updatedb
  2. Press Enter and provide your password.
  3. Wait a few seconds for the command to finish silently. The database is now current.

Step 3: Search for a File

Using the command is incredibly straightforward. You simply provide the name (or part of the name) of the file you are looking for.

  1. Type locate followed by your search term. For example, to find a file named “report.pdf”:
    locate report.pdf
  2. Press Enter.
  3. The terminal will instantly print the absolute paths of any files or directories that match that name across your entire system.

Step 4: Use Helpful Search Flags

You can modify how locate searches by adding specific flags.

  • Ignore Case (-i): By default, Linux is case-sensitive. “Report.pdf” is different from “report.pdf”. To search for a word regardless of capital letters, use the -i flag:
    locate -i report.pdf
  • Limit Results (-n): If your search term is very common, locate might return hundreds of results. You can limit the output to a specific number using -n. To only see the first 5 matches:
    locate -n 5 "report"

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.