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:
/var/lib/mlocate/mlocate.db
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"
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’.
Practical Examples of Locate Command
1. Search a File using locate
To search a particular file using locate, just do the following
srini@linuxforfreshers.com:~$ locate mysql.conf
/etc/init/mysql.conf
The following command searches for apache2.conf in the entire system.
srini@linuxforfreshers.com:~$ locate apache2.conf
/etc/apache2/apache2.conf
You can also use “locate -0” to display all the output in one line. For example:
Locate -0 apache2.conf
2. Display only the Count
To get the count of number of matching entry, use locate -c as shown below.
srini@linuxforfreshers.com:~$ locate -c apache2.conf
1
3.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
/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..)
srini@linuxforfreshers.com:~$ locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/alternatives/vncpasswd
/etc/alternatives/vncpasswd.1.gz
/etc/cron.daily/passwd
4. 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
# 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
/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
/tmp/NEW.txt
/tmp/new.txt
/usr/share/doc/samba-common/WHATSNEW.txt.gz