Jul 122012

I just realized that I’ve never written anything about one of my favorite commands on the Linux shell: locate.
This command take as input the name (or part of it) and instantly it gives you all the locations where that file is located, an alternative to this could be to use the command find, but there is an huge difference in the time that takes find to search large file systems in comparison to a locate, this difference is given from the fact that locate uses his own database where it stores all the file names, while find scan the directory and looks for the file name you have given as input.

mlocate.db the database of locate

To have a consistent output, you must have an updated database with the lists of the files. The operating system can be configured to do this automatically with a cron job. For example on my Ubuntu 12.04 I’ve this job scheduled with the file /etc/cron.daily/mlocate which is responsible for updating the database once a day.

If the default of your distribution is not efficient, the update can be done manually through the command sudo updatedb (require root access), this is very convenient when you have just installed some packages and maybe you look for a specific file, the time to perform an updatedb is always less than a find on the whole system.

The database is usually placed in


and you can control which file and/or filesystem scan while building this database with its configuration file located in


This is the content of mine:

# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs"
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 curlftpfs ecryptfs fusesmb devtmpfs"

When a directory is matched by PRUNEFS, PRUNENAMES or PRUNEPATHS, updatedb does not scan the contents of the directory. The
path of the directory itself is, however, entered in the created database. These are in details the options that you can use in this file:

  • 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.

using locate

Now that we have set up our database we can use the locate command from the command line (as user or as root, as you prefer) these are some examples on how to use this useful tool:

Basic usage – searching a file

$ locate php.ini

It would produce a list of the locations where you could find files that are named as php.ini. This might produce results as follows


Really useful when you search for that php option and don’t remember where exactly the php.ini file is located.

Suppress error messages

You could use the -q option to suppress error messages. Error messages would typically be messages stating that permission to access files were not allowed since you are only a user (not superuser). The -q option would suppress any other error messages as well

$ locate .php -q

Limit the ouput

You could use the -n option to limit the number of returned results to a specific number. E.g. you could ask for only 10 search results by the following command

$ locate .php -n 10

This would return the first 10 files that end in .php.

Count the number of results

You could use the -c option to just have the total number of results for a specific pattern search, in the following example I count all the files with a .php extension:

$ locate .php -c

Case insensitive

You could use the -i option in case you want to perform a case insensitive search. The case of the filenames would not be considered

$ locate INDEX.php -i

This will match to any of these files name:

index.php INDEX.PHP Index.Php

Get info on the database

And as last option, you can get statistics on your database with the option –statistics, this will not do any search but will print informations on your mlocate.db

$ locate --statistics
Database /var/lib/mlocate/mlocate.db:
	6146 directories
	57770 files
	3564748 bytes in file names
	1612746 bytes used to store database


This command has saved me many times on many different situations, and is a must have on any server or desktop that I use, I’ve not included an installation part in this short article, as locate should be available in any Linux distribution, usually in a package with name mlocate, and so you can easily use your package manager to install it, and I’m sure you’ll love it.

Popular Posts:

Flattr this!

  7 Responses to “Find files on Linux with the command locate”

  1. It’s a perfect command! Before that I read this post, I searched files like this:
    find / | grep filename
    But locate command is really faster than find!

  2. “I just realized that I’ve never wrote anything”

    Shurely u meen “rotten”????……

    Hope this helpsh….

  3. well done, good reminder of a great command. thanks for posting it.

  4. (1) The pedant should be satisfied with “I just realized that I’ve never written anything about one of my favorite commands on the Linux shell: locate”

    (2) The database can be manually updated with <code sudo updatedb

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>