Linux locate command tutorial

Linux locate command is a search utility in Linux. It can be found by default in almost all Linux distributions including Slackware and Ubuntu Linux. This tutorial is a basic locate command guide for a new Linux user. If you view locate command in Linux manual page, you will see the slocate command manual instead. The Linux locate command manual page has been replaced with slocate, the secure locate command. Don't worry about that because you can use the locate command with the help of this tutorial.

When you first run locate command from Linux command line terminal, you'll get fatal error like the example below:

[email protected]:~# locate php.ini-recommended
locate: fatal error: Could not find user database '/var/lib/slocate/slocate.db': No such file or directory
[email protected]:~#

Don't worry, that does not means your Linux locate command is not working. The error stated that '/var/lib/slocate/slocate.db' could not be found. That means Linux doesn't have locate database yet. What you can do is to run updatedb command. Login into command line terminal and su - to gain root privilege. See an example below for the complete steps:

[email protected]:~$ su -
[email protected]:~# updatedb

If you are using Ubuntu, use sudo updatedb command and provide your password.

The database update process will take some times. When it finished, you can try using Linux locate command again to search for file. To find a file in Linux system using locate command or to locate a file, we just invoke locate filename from the command line terminal.

Below is an example on how to search file using Linux locate command:

[email protected]:~# locate php.ini-recommended
[email protected]:~#

Sometimes you'll get warning when running locate command such as the example below:

[email protected]:~# locate php.ini-recommended
locate: warning: database /var/lib/slocate/slocate.db' is more than 8 days old
[email protected]:~#

As mentioned in the locate warning, your database has not been update for several days. The effect is the latest data has not been update into the database yet. So the locate command won't find the latest data. You have to run the updatedb command again to update the database. You can write a script to tell Linux cron to run updatedb everyday if you want.

If you want to use Linux slocate command instead of the locate command, you can manually install it using package management system. If you are using Ubuntu, you can use apt-get or aptitude from desktop. Below is an example on how to install slocate using apt-get command in Ubuntu command line:

[email protected]:~$ sudo apt-get install slocate
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
swh-plugins libqt4-dbus libboost-thread1.34.1 libboost-date-time1.34.1
python-openssl libqtcore4 libqt4-sql libphonon4 libqt4-xml libqt4-network
libqt4-designer libqtgui4 libboost-filesystem1.34.1 libqt4-script
libquicktime1 libmlt-data
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 32.0kB of archives.
After this operation, 156kB of additional disk space will be used.
Get:1 intrepid/main slocate 3.1-1.1ubuntu4 [32.0kB]
Fetched 32.0kB in 2s (11.5kB/s)
Selecting previously deselected package slocate.
(Reading database ... 131308 files and directories currently installed.)
Unpacking slocate (from .../slocate_3.1-1.1ubuntu4_i386.deb) ...
Processing triggers for man-db ...
Setting up slocate (3.1-1.1ubuntu4) ...
Adding group `slocate' (GID 127) ...

WARNING: You should run '/etc/cron.daily/slocate' as root. locate will not work
properly until you do or until it is run by cron (it is daily).

[email protected]:~$

We can remove package suggested by Ubuntu using apt-get autoremove command. Here is the example:

[email protected]:~$ sudo apt-get autoremove swh-plugins libqt4-dbus libboost-thread1.34.1 libboost-date-time1.34.1
[sudo] password for luzar:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
python-openssl libqtcore4 libqt4-sql libqt4-xml libqt4-network libqtgui4
libboost-filesystem1.34.1 libquicktime1 libmlt-data
The following packages will be REMOVED:
libboost-date-time1.34.1 libboost-filesystem1.34.1 libboost-thread1.34.1
libmlt-data libphonon4 libqt4-dbus libqt4-designer libqt4-network
libqt4-script libqt4-sql libqt4-xml libqtcore4 libqtgui4 libquicktime1
python-openssl swh-plugins
0 upgraded, 0 newly installed, 16 to remove and 0 not upgraded.
After this operation, 31.0MB disk space will be freed.
Do you want to continue [Y/n]? Y
(Reading database ... 131319 files and directories currently installed.)
Removing libboost-date-time1.34.1 ...
Removing libboost-filesystem1.34.1 ...
Removing libboost-thread1.34.1 ...
Removing libmlt-data ...
Removing libphonon4 ...
Removing libqt4-designer ...
Removing libqt4-script ...
Removing libqt4-dbus ...
Removing libqt4-network ...
Removing libqt4-sql ...
Removing libqt4-xml ...
Removing libqtgui4 ...
Removing libqtcore4 ...
Removing libquicktime1 ...
Removing python-openssl ...
Removing swh-plugins ...
Processing triggers for libc6 ...
ldconfig deferred processing now taking place
Processing triggers for python-support ...
Processing triggers for doc-base ...
Processing 1 removed doc-base file(s)...
Registering documents with scrollkeeper...
[email protected]:~$

Run /etc/cron.daily/slocate command to activate slocate and then run slocate -u option to create slocate database starting at path /:

[email protected]:~$ sudo /etc/cron.daily/slocate
[email protected]:~$ sudo slocate -u
[sudo] password for luzar:

Below is information about slocate command that you can find in manual page:

slocate - Security Enhanced version of the GNU Locate

slocate [-qi] [-d
] [--database=
slocate [-i] [-r ] [--regexp=]
slocate [-qv] [-o ] [--output=] slocate [-e
] [-f ] ] [-c] ]
slocate [-Vh] [--version] [--help]

Secure Locate provides a secure way to index and quickly search for all
files on your system regardless of ownership. It uses incremental
encoding just like GNU locate to compress its database to make search-
ing faster, but it will also check file permissions and ownership
before displaying matched entries so that users will not see files they
do not have access to. Note that permissions and ownership are not
stored in the database.

This manual page documents slocate. slocate enables system users to
search entire filesystems without displaying unauthorized files.

Here is an example using slocate command to find file in Linux system:

[email protected]:~$ slocate -qi my.cnf

Here is another example of slocate command:

[email protected]:~$ sudo slocate -U /etc
[sudo] password for luzar:
[email protected]:~$ slocate -qi --database=/etc samba
[email protected]:~$

First we create slocate database starting at path /etc directory. Then we issue slocate command to find a file or term in the database. This is a great option if you want to find a certain term in a particular directory.

That's all.

Back to Linux basic commands main page.

Add new comment