Monday, September 14, 2009

Searching Ubuntu filesystem with (m)locate

Ubuntu keeps a database of all the files in the file system at /var/lib/mlocate/mlocate.db. ( how this database is kept is dictated by /etc/updatedb.conf). The locate command
allows you to search that database. ( On Ubuntu, the locate command is a symbolic link
to mlocate. So you can also use mlocate )
You can use locate command to find out commands, devices, man pages, data files, or anything else identified by a name in the file system.
See the following example

The results come back instantly, since
the database is searched and not the actual file system. Before locate was available,
most Linux users ran the find command to find files in the file system. locate is case sensitive unless you use the –i option. Here’s an example:
$ locate -i cpuinfo
/usr/lib/python2.5/site-packages/numpy/distutils/cpuinfo.py
/usr/lib/python2.5/site-packages/numpy/distutils/cpuinfo.pyc
/usr/lib/python2.6/dist-packages/numpy/distutils/cpuinfo.py
/usr/lib/python2.6/dist-packages/numpy/distutils/cpuinfo.pyc


The mlocate package includes a cron job that runs the updatedb command once per day to update the locate database of files.
To update the locate database immediately, you can run the updatedb command manually.
$ sudo updatedb

No comments: