OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

locate – Find Files The Easy Way

The locate program performs a rapid database search of pathnames, and then outputs every name that matches a given substring. Say, for example, we want to find all the pro - grams with names that begin with “zip.” Since we are looking for programs, we can as- sume that the name of the directory containing the programs would end with “bin/”. Therefore, we could try to use locate this way to find our files:


[me@linuxbox ~]$ locate bin/zip

[me@linuxbox ~]$ locate bin/zip


locate will search its database of pathnames and output any that contain the string


“bin/zip”:



/usr/bin/zip

/usr/bin/zipcloak

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipnote

/usr/bin/zipsplit

/usr/bin/zip

/usr/bin/zipcloak

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipnote

/usr/bin/zipsplit


If the search requirement is not so simple, locate can be combined with other tools such as grep to design more interesting searches:


[me@linuxbox ~]$ locate zip | grep bin

/bin/bunzip2

/bin/bzip2

/bin/bzip2recover

/bin/gunzip

/bin/gzip

/usr/bin/funzip

/usr/bin/gpg-zip

/usr/bin/preunzip

/usr/bin/prezip

/usr/bin/prezip-bin

/usr/bin/unzip

/usr/bin/unzipsfx

/usr/bin/zip

/usr/bin/zipcloak

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipnote

/usr/bin/zipsplit

[me@linuxbox ~]$ locate zip | grep bin

/bin/bunzip2

/bin/bzip2

/bin/bzip2recover

/bin/gunzip

/bin/gzip

/usr/bin/funzip

/usr/bin/gpg-zip

/usr/bin/preunzip

/usr/bin/prezip

/usr/bin/prezip-bin

/usr/bin/unzip

/usr/bin/unzipsfx

/usr/bin/zip

/usr/bin/zipcloak

/usr/bin/zipgrep

/usr/bin/zipinfo

/usr/bin/zipnote

/usr/bin/zipsplit


The locate program has been around for a number of years, and there are several dif- ferent variants in common use. The two most common ones found in modern Linux dis- tributions are slocate and mlocate, though they are usually accessed by a symbolic link named locate. The different versions of locate have overlapping options sets. Some versions include regular expression matching (which we’ll cover in an upcoming chapter) and wildcard support. Check the man page for locate to determine which ver- sion of locate is installed.


image

Where Does The locate Database Come From?

You may notice that, on some distributions, locate fails to work just after the system is installed, but if you try again the next day, it works fine. What gives? The locate database is created by another program named updatedb. Usu- ally, it is run periodically as a cron job; that is, a task performed at regular inter- vals by the cron daemon. Most systems equipped with locate run updatedb once a day. Since the database is not updated continuously, you will notice that very recent files do not show up when using locate. To overcome this, it’s pos- sible to run the updatedb program manually by becoming the superuser and running updatedb at the prompt.


Top OS Cloud Computing at OnWorks: