Hello! Welcome to the next lesson in our "Text Processing and File Searching" module.
In our previous session, you learned how to use grep to search for text inside of files. That's a powerful skill for finding specific content. But what if your challenge is different? What if you know the name of a file (or part of its name) but have no idea where it is on the system?
This is where today's command, locate, comes in. This lesson is dedicated to learning how to use locate for extremely fast file searches. We'll cover how it works using a pre-built database, why this makes it so fast, and the crucial trade-off you need to be aware of. Mastering locate will give you a speedy tool for finding files, a common task for any Linux administrator and a key topic for your CompTIA Linux+ exam.
1. What is locate and How Does It Work?
The locate command is designed for one primary purpose: to find files on your system by name, and to do it very, very quickly. Unlike other search commands that scan your filesystem in real-time, locate consults a pre-built database of all the files and directories on your system. Think of it like using the index at the back of a book instead of flipping through every single page.
To get started, let's watch a video that introduces the command, explains how you can install it if it's not already on your system, and demonstrates its basic usage.
Quickly Find Any File in Linux with the locate Command
This video from Learn Linux TV explains what the locate command is, how to install its package (plocate or mlocate), and its basic usage.
Watch from the beginning to 04:04. This will cover the purpose of locate, how to install it if needed, and how to perform a basic search. Notice that you don't need to specify a starting directory; locate searches its entire database by default.
As you saw, the basic syntax is straightforward:locate [search_term]
The command will then output every path in its database that contains the search term.
2. The Database: The Heart and Soul of locate
The speed of locate is its greatest advantage, but it comes with an important trade-off: the search results are only as current as the database itself.
Most Linux systems run a scheduled task (a cron job) to update this database automatically, usually once a day. This means that if you create or download a new file, locate won't be able to find it until the next automatic update.
Fortunately, you can manually update the database at any time using the updatedb command. Because this command needs to scan the entire filesystem, it typically requires administrator privileges, so you'll run it as sudo updatedb.
The following resources perfectly illustrate this core concept.
This video from Linux Training Academy clearly demonstrates why new files don't show up immediately and how the updatedb command is used to solve this.
Watch from 00:58 to 03:02. Pay close attention to the sequence of creating a file with touch, seeing locate fail, running updatedb, and then seeing locate succeed.
This workflow is so fundamental to using locate that it's worth seeing again in a static image.

Test your understanding!
You just created a backup script and saved it as backup_script.sh in your home directory. You immediately run locate backup_script.sh but get no output. What is the most likely reason for this, and what is the exact command you should run to fix it?
Show answer
The most likely reason is that the locate database has not been updated since the file was created. The search is fast because it's querying a pre-existing index, not the live filesystem.
To fix this, you should manually update the database with the command: sudo updatedb
After the update completes, running locate backup_script.sh again should successfully find your script.
3. Practical Usage and Common Options
Now that you understand the underlying mechanism, let's explore some common options that make locate even more effective for daily tasks.
-i(ignore case): This makes your search case-insensitive.locate -i myfilewill findMyFile,myfile, andMYFILE. This is extremely useful when you can't remember the exact capitalization of a file.-c(count): Instead of printing a list of all matching files, this option simply tells you how many matches were found.*(wildcard/globbing): You can use wildcards just like with other commands. For example,locate '*.conf'will find all files in the database that end with the.confextension.
Let's see these options in action.
Quickly Find Any File in Linux with the locate Command
Let's return to the Learn Linux TV video to explore some common options that make locate more flexible.
Watch from 06:23 to 09:02. This segment covers case-insensitive searching (-i), using wildcards (*), counting results (-c), and limiting output.
While we've covered the most essential options, there are others available. The following article provides a handy reference table and reinforces the concepts with more examples.
For a comprehensive list of options and more examples, the article "The locate command in Linux" from ComputerNetworkingNotes is an excellent reference. You don't need to memorize all of these, but it's good to be aware of what's possible.
First, quickly scan the table under the heading "The locate command options" to see the variety of flags available. Then, read the examples under "The locate command examples," focusing on the sections for case-insensitivity (-i), counting (-c), and limiting results (-n).
Conclusion
In this lesson, you've added a valuable and speedy tool to your file-searching toolkit. You now understand how to use locate to find files by name almost instantly.
Key Takeaways:
- The
locatecommand finds files by searching a pre-built database, which makes it extremely fast. - The main drawback is that the database can be outdated. Recently created files won't appear in search results.
- Use
sudo updatedbto manually refresh the database to ensure it's current. - Essential options include
-ifor case-insensitive searches and-cto get a count of matching files.
So, locate is fast but can be inaccurate. What if you need to find files with 100% accuracy? Or what if you want to search based on criteria other than just the name, like file size, ownership, or modification time? That's where the find command comes in. In our next lesson, we will explore this powerful, flexible, and real-time search tool.