Search an Indexed Filename Database
Search an Indexed Filename Database
You need an attendance file quickly and have only part of its name. locate can search a filename database rather than walk the live filesystem for every query. That can make name searches fast, especially across many directories. But a database is a record from an earlier scan, not a live view.
Let's treat that trade-off honestly. A newly created report may be missing from the index, and a deleted report may still appear until the index updates. The exact tool, database policy and update mechanism depend on the Linux distribution.
What you’ll learn
- Search an indexed filename database with locate.
- Choose a live find search when an index may be outdated.
A database search depends on its last update
Earlier scan
A database records filenames present when it was built.
indexed pathsNew file
A report created later may not yet have a database entry.
attendance.txt addedQuery
locate reads the database rather than rescanning every live directory.
possibly missing result
Search the filename index for attendance.txt
Prepare the example
This example needs an installed locate implementation and a usable database. An empty exercise folder alone does not prepare that database. If the utility or database is missing, use your distribution's documentation to set it up; do not assume an empty result proves that an attendance file is absent.
Bash · Linux terminal
locate attendance.txtExpected behavior; machine-dependent details
Matching indexed paths vary by machine.locate attendance.txt asks the configured index for matching names. Results can contain paths outside your current directory because the search is about indexed filenames, not just the folder you are standing in. Inspect the returned paths before opening anything.
Now imagine creating a new attendance file after the database was built. A live find search beneath its folder can discover it immediately, while locate may not know about it yet. Updating the database can require scheduled maintenance or administrative access. For this lesson, the important skill is choosing the right search model, not forcing every tool to return the same result.
The indexed search and its setup requirements
locate is designed for fast filename searches. Instead of walking through directories every time, it looks in a database of known paths.
What is locate?
The locate command helps you find the full path of files or directories by searching a prebuilt database. Unlike find, which scans the filesystem in real time, locate is much faster because it uses an indexed list of all files on your system.
Step 1: Install locate (if not already installed)
First things first—let's install mlocate, which provides the locate command.
sudo apt update
sudo apt install mlocate
For Red Hat-based systems:
sudo yum install mlocate
Step 2: Update the locate database
Before you can use locate, you need to update its database. This step collects all the paths on your system so locate knows where to look.
sudo updatedb
This might take a few seconds depending on your system size.
Patterns, filters and database freshness
Step 3: Find a file using locate
Now, let’s say you want to find a file named notes.txt. You simply run:
locate notes.txt
You’ll see something like:
/home/alex/Documents/notes.txt
/home/alex/Downloads/old/notes.txt
Wow, that was fast! Unlike find, you don’t have to specify a directory—it searches the entire system.
Step 4: Use wildcards with locate
Want to find all files that end with .log? Use a wildcard:
locate "*.log"
Note: You may need to wrap the pattern in quotes to prevent the shell from expanding it too early.
Step 5: Filter results with grep
If your search returns too many results, pipe it through grep to filter them:
locate notes | grep "2024"
This will show only results that contain "2024" in their paths.
Run updatedb regularly
Because locate depends on a database, new files won't show up until the database is updated. You can automate this by setting up a cron job, or just run sudo updatedb manually every now and then.
Keep the locate database current
Now you know how to use the locate command to find files in seconds. Try it out and see how much time it saves you!
Try it, then check the worked answer
Your task: Choose a search that sees a newly created file immediately.
This example needs an installed locate implementation and a usable database. An empty exercise folder alone does not prepare that database. If the utility or database is missing, use your distribution's documentation to set it up; do not assume an empty result proves that an attendance file is absent.
Bash · complete example for this question
locate attendance.txtExpected behavior; machine-dependent details
Matching indexed paths vary by machine.Worked answer
find school -name attendance.txt
Use a live find search when you must see a newly created file now. locate may be faster for an indexed name, but freshness is part of its trade-off. A missing index result is evidence about the database, not a definitive statement about the current filesystem.
Check your decision
An index can miss a file that already exists
Choose locate for a convenient indexed name search and find when current filesystem state or richer conditions matter. Check the freshness and scope of the index before drawing conclusions. Search results tell you where to investigate; they are not a guarantee that every returned path still exists.
Reaching the recap and completing the quizzes are separate milestones.