Finding Files with locate

Many Linux users use the ‘find’ utility when searching for files using the command line on their system. They’ll do a simple:

find / -name 'pattern'

Really though, the power of find isn’t just in finding names of files but rather specific details about those files. For example, if you wanted to find files which are writable by both their owner and their group:

find / -perm -444 -perm /222 ! -perm /111

or perhaps find any file that’s been altered in your Download directory in the past 24 hours:

find /home/user/Downloads/ -mtime 0

As you can see, the find command is very versatile and can be used to find an array of different attributes of files.  There are times though where I’m just looking for something and I don’t want to have to wait for the command to scan the entire directory tree in order to track it down.  That’s where locate comes in with quick and simple results.

Using the Locate Command

Using the locate command can only be accomplished if you install the mlocate package.  Most major distributions have this available.  If not, head over to the mlocate homepage and install manually.  Once that is accomplished, you’ll need to manually run a command to index your filesystem with it…otherwise, you’ll have to wait for the command to run automatically as it registers with cron to do so on a system level.  Open an terminal and change to your root user, then execute the following:

updatedb &

This updates the mlocate database that indexes your files and forks it to the background (the ‘&’ forks it to the background).  You can now logout of the terminal as root and the process will quietly work in the background.

After the command completes, using mlocate is as easy as using the locate command:

locate firefox | less

The command above will look for all files with firefox in the name and pipe the command through less so you can use the spacebar or enter key to scroll the file buffer.  Of course, the reason we pipe it through less is because any file that resides in the ‘firefox’ directory will be reported in the output.  While this tool isn’t as granular as the find command, it is a quick way to track down paths, directories, and files you know should exist.  Since the data is indexed using the updatedb command (by cron) the results are very quick and the command does not have to scan through the filesystem to return the results.

There are plenty more advanced options via flags (such as following symbolic links, making search term case sensitive, and even using regexp).  See the man page for details on how each of these options work.  Play around with locate and see what you can do!  It’s a powerful and quick search command!

Author: devnet

devnet has been a project manager for a Fortune 500 company, a Unix and Linux administrator, a Technical Writer, a System Analyst, and a Systems Engineer during his 20+ years working with Technology.

10 thoughts on “Finding Files with locate”

  1. When I was first learning the Linux terminal, locate was one of those commands that gave me one of those A-HA! moments where you realize why people go on about how cool and useful and powerful the command line can be. So simple, yet so useful.

    Too bad it can’t tell me what I did with the digital copy of my resume….

    1. lol! Yes, it would be nice to have a blended find+locate. I’m sure there is already one out there but it would be pretty nice to have one that is quick like locate that doesn’t take up so much system resources.

  2. Hey Dude,

    Here is some more stuff you can do with find

    Delete Files Older Than ‘x’ Days on Linux using find:
    find /path/to/files* -mtime +5 -exec rm {} \;

    Adding -f forces the delete:
    i.e. find /path/to/files* -mtime +5 -exec rm -f {} \;

    Moving instead of deleting:
    i.e. find /path/to/files/* -mtime +30 -exec mv {} /path/to/newfolder/ \;

    See Delete Files Older Than ‘x’ Days on Linux for more info.

  3. smart,its nice to have a find+locate.i strongly believe that its very nice nice to have one that is quick like locate and doesn’t take up so much system resources…

Comments are closed.

Creative Commons License
Except where otherwise noted, the content on this site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.