≡ Menu

7 Practical Linux Locate Command Examples – mlocate and updatedb

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"

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.

# updatedb

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.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Ashok March 14, 2012, 2:33 am

    “locate” is one of my favorite command in Linux and reading an article on it was a pleasure. Thanks !!

  • Flavio March 14, 2012, 2:52 am

    Thanks for the articles. Always clear, useful and straightforward. I am learning a lot from thegeekstuff tips and that’s how the knowledge flows in an open source community.

  • Chris F.A. Johnson March 14, 2012, 1:13 pm

    One problem with locate is that it will find not only the filename you give it on the command line, but also any files that contain that string. To prevent that, I use a function, flocate:

    flocate() { locate “*/$1”; }

  • Ashok March 14, 2012, 9:01 pm

    Hi Chris, I must disagree with you. “locate” command find only the filenames and NOT the files that contain the input string. I even did a small test as shown below.

    # locate nsswitch.conf
    /etc/nsswitch.conf
    /usr/share/doc/yp-tools-2.9/nsswitch.conf
    /usr/share/man/man5/nsswitch.conf.5.gz
    # vi /opt/test
    # cat /opt/test
    nsswitch.conf
    # updatedb
    # locate nsswitch.conf
    /etc/nsswitch.conf
    /usr/share/doc/yp-tools-2.9/nsswitch.conf
    /usr/share/man/man5/nsswitch.conf.5.gz
    #

    To list the files contains a particular string, please use the one of syntax given here.

  • Diop March 15, 2012, 4:57 am

    Hi
    Many Thanks for this topic, its very helpful.
    Can you talk me about find command and his difference with locate and what is the better.
    Thanks

  • Chris F.A. Johnson March 16, 2012, 12:46 pm

    Ashok, try: locate pass

    You will get all files whose name includes the string “pass”, such as /etc/passwd.

  • Vadivel Murugan Vattappan March 19, 2012, 11:24 am

    Very nice and useful tips about locate command. I am using the locate and find commands effectively. This article is very much useful for me. Thank you and provide more options.

  • Kostya March 31, 2012, 8:12 am

    Hi! Thanks for the article. I have a question. How I can use locate command with rm command?

    Thank you!

  • manvstech April 2, 2012, 7:55 am

    @Kostya: to search for and remove files, you are probably better off using ‘find’. Example:
    # find . -type f -name “string” -print | xargs rm

  • Kostya April 5, 2012, 1:35 pm

    Hi! Thanks for the answer.
    I have new question: Is it possible to use a common database locate, which is stored on a file server (Linux or Windows)?

  • Ashok April 23, 2012, 1:08 am

    @Kostya, It is possible but it doesn’t make sense. ‘locate’ is an utility provided to locate the local files on your machine in a Faster way. The “mlocate.db” doesn’t take much space even for a commercial DB server and even if it lost, we can always recreate it by executing ‘updatedb’ command. So what is the point in keeping the mlocate DB on a file-server?

  • moi May 20, 2012, 5:12 pm

    thanks, now I finally know why locate finds nothing in my /media directory and how to fix that 🙂

  • Dave Burton May 12, 2013, 10:24 am

    The (slow) Linux “find” command has an option, “-ls”, to display size, date, etc. like the “ls -l” command. But the “locate” command doesn’t seem to have that. So how can I get the equivalent functionality with locate?

    I’ve used back-ticks to pass the output of locate to ls, like this:

    ls -al `locate -e somefile`

    …which works as long as somefile exists. But if somefile doesn’t exist, it gives me a full directory listing.

    If I do this:

    ls -al `locate -e somefile` thisfileneverexists

    …then it sort of works, if you don’t mind the error line:

    ls: cannot access thisfileneverexists: No such file or directory

    …which leads us to the obvious-but-extremely-ugly workaround:

    ls -al `locate -e somefile` thisfileneverexists 2>/dev/nul

    That works, but surely there’s a better way!

  • Nick April 3, 2015, 7:06 pm

    @ Ashok (comment 4):
    Chris (comment 3) meant that locate finds the filename string you give it and OTHER LONGER FILENAMES that contain the string.

  • komal August 14, 2015, 5:37 am

    when i insert any locate command on command prompt then it gives me an error as:
    locate: can not stat () `/var/lib/mlocate/mlocate.db’: No such file or directory
    then what should i have to do.

  • Mike February 1, 2016, 11:46 pm

    Is the Updatedb a cron job? When does the OS run the updatedb command? What would be the advantages of setting this command up in a script to run at each logon? I have been using Linux for some time now and never knew about the updatedb command. Thanks! :O)

  • Pankaj February 1, 2017, 11:41 pm

    @ Mike
    # ls -Fl /etc/cron.daily
    -rwx——. 1 root root 118 2016-05-11 10:30 cups*
    -rwx——. 1 root root 180 2003-07-10 01:06 logrotate*
    -rwxr-xr-x. 1 root root 905 2013-02-22 07:43 makewhatis.cron*
    -rwx——. 1 root root 189 2015-01-26 16:18 mlocate.cron*
    -rwxr-xr-x. 1 root root 2126 2013-07-19 20:05 prelink*
    -rwxr-xr-x. 1 root root 563 2013-11-23 19:22 readahead.cron*
    -rwxr-xr-x. 1 root root 433 2015-11-07 00:04 tmpwatch*

    [root@nptest cron.daily]# cat mlocate.cron
    #!/bin/sh
    nodevs=$(/dev/null 2>&1
    ionice -c2 -n7 -p $$ >/dev/null 2>&1
    /usr/bin/updatedb -f “$nodevs”