Enabling Index Based File Search
Technical Background
Searching through the entire file system with tools like find can be slow because it scans directories in real-time. Index-based search tools like plocate use a pre-built database of file names that allows instant lookups. However, the index only reflects the file system state at the time it was last updated, meaning new files won’t appear and deleted files might still show up until the index is refreshed.
Related Links
Solution
Setup
- Update your packages and install
plocate
:
apt update
apt install plocate
- Build the initial file index:
updatedb
Info
This creates the file index and builds a database at /var/lib/plocate/plocate.db
Warning
locate
returns stale until this command is run
Using File Index
- Search for an existing files:
locate aptitude
Note
You should see results from already indexed files.
- Creating a new file:
touch ~/mylocaltest.txt
- Search without updating the index:
locate localtest
Note
You will see no result, because the index doesn’t know about this new file yet.
- Update the index:
updatedb
locate localtest
Note
Now you should see: /home/<username>/mylocaltest.txt
Warning
Some files get excluded by the index.
- Delete the file:
rm ~/mylocaltest.txt
- Search without updating the index:
locate localtest
Note
You will still see the file listed because the index still contains its old entry.
- Rebuild the index and search again:
updatedb
locate localtest
Note
This time, there is no output, as the deleted file has been removed from the index.