Custom Search

Ubuntu search files using locate command

Ubuntu locate files tutorial is a guide on how to use locate command to find files in the system.

This is a part of locate manual page:

locate - find files by name

locate reads one or more databases prepared by updatedb(8) and writes filenames
matching at least one of the PATTERNs to standard output, one per line.

Here is an example of how to use locate command to find file in Ubuntu system:

luzar@ubuntu:~$ sudo locate mkdir
[sudo] password for luzar:
locate: can not open `/var/lib/mlocate/mlocate.db': No such file or directory

Oppss...what's wrong? Well, that's a usual warning we'll get on the first time we use locate command. So we need to update mlocate database first. Use command updatedb to update mlocate.db like an example below:

luzar@ubuntu:~$ sudo updatedb

It'll take some times for updatedb command to finished update mlocate database. When it finished, try the locate command again.

luzar@ubuntu:~$ sudo locate mkdir


The command success this time. So what locate without any option does, it prints all matching mkdir it can find in the mlocate database. However, since locate command search files in the database, it can't tell if the files still exist. It also can't find new files that haven't been update into the database.

Ubuntu locate files using -c option example:

luzar@ubuntu:~$ locate -c mkdir
luzar@ubuntu:~$ locate -c adduser

The locate -c option prints result numbers instead of file names. The result numbers are the matching pattern locate has counted. Below is another example of locate -c but this time we use it with --basename option.

luzar@ubuntu:~$ locate --basename -c adduser

Now with another option:

luzar@ubuntu:~$ locate --wholename -c adduser

See the result different? The --basename match only the base name against the specified patterns. While the --wholename does the opposite.

Ubuntu locate files using -i option example:

luzar@ubuntu:~$ locate -i \file.txt

The locate -i option will find matching pattern ignoring case distinctions. Note that I used Linux glob character \ to find the matching characters.

That's it for now. Below is my final notes of locate command for you:

luzar@ubuntu:~$ ls -l
total 32-rw-r--r-- 1 luzar luzar 0 2008-10-21 22:16 File.txt
drwxr-xr-x 3 luzar luzar 4096 2008-10-23 05:00 folder
-rw-r--r-- 1 luzar luzar 268 2008-10-23 05:43 interfaces.bac
luzar@ubuntu:~$ mv interfaces.bac Interfaces.txt

I changed the file interfaces.bac to Interfaces.txt. There's no more interfaces.bac in the folder. Now, let's check with locate command to find the file.

luzar@ubuntu:~$ locate -i \interfaces

See that the interfaces.bac still in the database? Where is interfaces.txt?

luzar@ubuntu:~$ locate -i \interfaces.txt

No Interfaces.txt file. Why? I forgot to update mlocate database. Now, again:

luzar@ubuntu:~$ sudo updatedb
[sudo] password for luzar:
luzar@ubuntu:~$ locate -i interfaces

There it is. My final tip, update the mlocate database frequently.

No comments:

Post a Comment

Please keep comment relevant and strictly no spam will be tolerated. Thank you.