When you need to search for some files, you might typically use find command. find is a good search utility but it is slow.
However locate can search for files very quickly.
Though the locate command works very fast, it still has not out-thrown the find command because it has some limitations.
This article explains everything you need to know about locate command .
How Locate Command Works? – updatedb and updatedb.conf
When we say that locate searches very quickly, then the first question that comes into mind is that what makes locate so fast?.
Well, locate does not search the files on disk rather it searches for file paths in a database.
The database is a file that contains information about the files and their path on your system. The locate database file is located at:
The next logical question is, what keeps this mlocate database updated?
Well, there is another utility known as updatedb. When you execute updatedb, it scans the whole system and updates the mlocate.db database file.
So one limitation of the ‘locate’ command is its dependency on the database which can be updated by another utility ‘updatedb’. Hence, in order to get the latest and reliable results from ‘locate’ command the database on which it works should be updated at regular intervals.
We can also configure the ‘updatedb’ utility as per our needs. This can be achieved by updating the updatedb.conf. This is a configuration file that updatedb reads before updating the database. updatedb.conf is located under /etc/ :
# cat /etc/updatedb.conf PRUNE_BIND_MOUNTS="yes" PRUNENAMES=".git .bzr .hg .svn" PRUNEPATHS="/tmp /var/spool /media" PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs"
updatedb.conf file contains information in the form of VARIABLES=VALUES. These variables can be classified into :
- PRUNEFS : A whitespace-separated list of file system types (as used in /etc/mtab) which should not be scanned by updatedb. The file system type matching is case-insensitive. By default, no file system types are skipped. When scanning a file system is skipped, all file systems mounted in the subtree are skipped too, even if their type does not match any entry in PRUNEFS.
- PRUNENAMES : A whitespace-separated list of directory names (without paths) which should not be scanned by updatedb. By default, no directory names are skipped. Note that only directories can be specified, and no pattern mechanism (e.g. globbing) is used.
- PRUNEPATHS : A whitespace-separated list of path names of directories which should not be scanned by updatedb. Each path name must be exactly in the form in which the directory would be reported by locate. By default, no paths are skipped.
- PRUNE_BIND_MOUNTS : One of the strings 0, no, 1 or yes. If PRUNE_BIND_MOUNTS is 1 or yes, bind mounts are not scanned by updatedb. All file systems mounted in the subtree of a bind mount are skipped as well, even if they are not bind mounts. By default, bind mounts are not skipped.
Note that all of the above configuration information can also be changed or updated through the command line options to the utility ‘updatedb’.
Before you try locate command examples, make sure to explore find command examples.
Practical Examples of Locate Command
1. Search a File using locate
To search a particular file using locate, just do the following
$ locate sysctl.conf /etc/sysctl.conf /usr/share/man/man5/sysctl.conf.5.gz
The following command searches for httpd.conf in the entire system.
$ locate httpd.conf /etc/httpd/conf/httpd.conf /usr/local/apache2/conf/httpd.conf /usr/local/apache2/conf/httpd.conf.bak
You can also use “locate -0″ to display all the output in one line. For example:
$ locate -0 httpd.conf
2. Display only the Count
To get the count of number of matching entry, use locate -c as shown below.
$ locate -c httpd.conf 3
3. Use updatedb to Refresh mlocate Database
Suppose you took a backup of an existing file:
# cd /etc # cp sysctl.conf sysctl.conf.orig
If you try to search for sysctl.conf using the ‘locate’ utility, you’ll not find the sysctl.conf.orig.
# locate sysctl.conf /etc/sysctl.conf /usr/share/man/man5/sysctl.conf.5.gz
The reason is that after the sysctl.conf.orig was create the database on which the locate utility works is not updated. So lets update the database using the ‘updatedb’ command and execute locate again:
$ updatedb updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'
Please note that update db needs to be executed as root.
After updatedb, if you execute locate, you’ll find the sysctl.conf.orig file.
# locate sysctl.conf /etc/sysctl.conf /etc/sysctl.conf.orig /usr/share/man/man5/sysctl.conf.5.gz
4. Change mlocate Database Location
The default database that locate utility reads is /var/lib/mlocate/mlocate.db, but if you wish to link the locate command with some other database kept at some other location, use the -d option.
For example :
$ locate -d <new db path> <filename>
Note that the database path can also be taken from stdin and if an empty path is mentioned then the default data base is picked.
5. Check File Existence
Now suppose the file sysctl.conf.orig that we created in example 3 above got deleted, and if you try to locate sysctl.conf, it will still display the sysctl.conf.orig file.
# cd /etc # rm sysctl.conf.orig # locate sysctl.conf /etc/sysctl.conf /etc/sysctl.conf.orig /usr/share/man/man5/sysctl.conf.5.gz
As you see from the above output, locate command shows sysctl.conf.orig even after the file was deleted. This result is mis-leading.
Ofcourse, you can execute updatedb, and try locate again, which will show proper results.
Or, you can just use ‘locate -e’, which will display only the files that exists in the system, when you are executing the locate command. i.e Even when the file exist in the mlocate.db, it will still verify to make sure the file is physically present in the system before displaying it.
# locate -e sysctl.conf /etc/sysctl.conf /usr/share/man/man5/sysctl.conf.5.gz
6. Ignore Case in Locate Output
The locate command by default is configured to accept the file name in a case sensitive manner. In order to make the results case insensitive, we can use the -i option :
In the following example, we created two files with both lowercase and uppercase.
# cd /tmp # touch new.txt NEW.txt # updatedb
If you use the locate command only with the lowercase, it will find only the lowercase file.
# locate new.txt /tmp/new.txt
Use locate -i, which will ignore case, and look for both lowercase and uppercase file.
$ locate -i new.txt /tmp/NEW.txt /tmp/new.txt /usr/share/doc/samba-common/WHATSNEW.txt.gz
7. Restrict the Locate Output
In the following example, locate command displayed several entries.
$ locate passwd /etc/passwd /etc/passwd- /etc/dovecot/conf.d/auth-passwdfile.conf.ext /etc/pam.d/passwd /etc/security/opasswd /etc/vsftpd/passwd /lib64/security/pam_unix_passwd.so /usr/bin/gpasswd /usr/bin/htpasswd /usr/bin/ldappasswd /usr/bin/mksmbpasswd.sh /usr/bin/passwd ..
If you want to display only certain number of records, use locate -l option and specify how many records you want to see in the locate command output.
For example, the following displays only 5 records (Even when locate command finds several records..)
$ locate -l 5 passwd /etc/passwd /etc/passwd- /etc/dovecot/conf.d/auth-passwdfile.conf.ext /etc/pam.d/passwd /etc/security/opasswd
Find Command Examples
As you see locate command examples are fairly straight forward to use. However, to get reliable output, and to perform some complex search operations, you should also know how to use find command effectively.