locate
On most Linux systems, the updatedb
command runs once per day to gather the names of files throughout your Linux system and stores them in a local database. You can then run the locate
command to search this database to find the location of named files stored within it.
Like anything, there are pros and cons to this approach...
Advantage | Disadvantage |
The | The |
Not every file in your filesystem is stored in the database - see below |
The contents of the /etc/updatedb.conf
file limit which filenames are collected by excluding specific mount types, filesystem types, file types, and mount points. For instance, filenames are excluded from remotely mounted filesystems like CIFS
and NFS
, as well as locally mounted CDs or DVDs in the iso9660 format. Additionally, paths containing temporary files in /tmp
and spool files in /var/spool/cups
are pruned.
You have the flexibility to add or remove items from the locate
database pruning list to customise it according to your requirements. For instance, in RHEL 8, the updatedb.conf
file includes the following:
As a regular user, you can't see any files from the locate database that you can't see in the filesystem normally. For example, if you can't type ls
to view files in the /root
directory, you can't locate
files stored in that directory either.
Using locate
When you search for a string, the string can appear anywhere in a file’s path. For example, if you search for passwd
, you could turn up:
/etc/passwd
/usr/bin/passwd
/home/example/passwd/pwdfiles.txt
...and any number of other places
If you add files to your system after updatedb
runs, you can’t locate those files until updatedb
runs again. To get the database to contain all files up to the current moment, you can simply run updatedb
from the shell, but this must be done with root privileges.
Here are some examples of using the locate command to search for files:
When run as a regular user, locate only finds .bashrc
in /etc/skel
and the user’s own home directory. However, when run as root, the same command locates .bashrc
files in everyone’s home directory.
Using locate -i
, filenames are found regardless of case sensitivity.
Last updated