top of page

Linux Locate Command - A Quick And Easy Way To Find Your Stuff

Mar 9

3 min read

1

3

0


Locate Command Missing?

There are two versions of the locate command.

mlocate and plocate are both used to find files but plocate is newer, faster, and more efficient. If you have the option, install plocate.


Let's check to see if it's installed.

which locate

If you don't get a reply such as, /usr/bin/locate. You need to install it using your package manager.


How To Use Locate


A good place to start is to quickly check the man page for something you are using or learning about. man locate


locate what-to-find


locate -i what-to-find

With the -i option, the case doesn't matter. I usually always use -i with locate and many other commands such as grep, find, etc.


Locate Directories

The locate command by default finds directories but if you search for let's say /etc. On my machine I get 6348 results with locate -c /etc


If I do, locate -n 1 /etc, it only displays the first result, which is the directory itself. Locate does have regex supports which is good for more complex searches.

locate --regex '^/etc$'


Advanced Locate Searches

Find all mp4 and avi files.

locate --regex -i "(\.mp4|\.avi)"


locate -b config

This will only find files where "config" appears in the filename itself.


This would return something like: /usr/src/kernels/6.13.5-200.nobara.fc41.x86_64/drivers/pmdomain/st/Kconfig


You see the Kconfig at the end. If you want to be more specific, and just locate "config" files. locate -b --regex '^config$'


The Database


Location of the database, at least on my computer is, /var/lib/plocate.


The database is built and updated by the updatedb command, which scans the entire file system and records the full paths of files and directories. The database is a binary file optimized for quick lookup's. It’s not human-readable.


The database only reflects the state of the file system at the last time updatedb was run. If files are added, deleted, or moved since then, the results may be outdated until the database is refreshed.


Let's say you created the file 80s-best-movies.txt. Then you delete the file. You have a bunch of files that start with 80s on your computer so you do, locate 80s. This will return 80s-best-movies.txt in the results until you either manually run updatedb, or until the systemd-timer runs that updates the database.


If you want to be sure no deleted files or directories are included use the -e option before the database is updated.

locate -e 80s will not return 80s-best-movies.txt.


Because it’s a snapshot, it won’t find files created after the last updatedb run. For real-time searches, you’d use the find command instead or just run the updatedb command.


The database isn’t updated automatically in real-time. Instead, updatedb is typically run periodically via a cron job or systemd timer. On my system it uses a systemd timer.


Let's find the name of the locate timer.

systemctl show timers.target | grep locate


Now we should have the name of the timer, we can check the journalctl log for information regarding when it ran and stopped. journalctl -u plocate-updatedb.timer


Typically timers will have a unit file with the same name as the timer. For instance, if you want to cat out the service for plocate it would be, systemctl cat plocate-updatedb.service.


Notice the name is the same, except the ending is different, .service, not .timer. You could have a different name for the service unit. If the service file does not have the same name as the timer you would have to specify a unit name for the service in the .timer unit. Unit=different-name.service.

Comments

Share Your ThoughtsBe the first to write a comment.

© 2035 by Maya Nelson.
Powered and secured by Wix

Call

123-456-7890

Write

Follow

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
bottom of page