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.
- Open your terminal (Ctrl + Alt + T).
- Attempt to run a simple locate command:
locate --version - 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 - Enter your password when prompted. (Note:
plocateis the modern, faster replacement for the oldermlocatepackage, but it still uses the samelocatecommand).
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.
- In your terminal, type the following command:
sudo updatedb - Press Enter and provide your password.
- 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.
- Type
locatefollowed by your search term. For example, to find a file named “report.pdf”:locate report.pdf - Press Enter.
- 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
-iflag:locate -i report.pdf - Limit Results (-n): If your search term is very common,
locatemight 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"