Linux: Searching for files with locate command

Easy method with locate command

The easiest way is to use locate and find the file you want.

Example:

root@bt:/# locate mp.sh
/home/passwd/mp.sh
/usr/bin/mp.sh

There are times when you cannot search file with locate program, this is because the path database of the file is not updated. You can invoke the update using updatedb command.

Another example with simple regexp:

locate -r gcc$

Result:

/etc/bash_completion.d/gcc
/root/.wine/drive_c/MinGW/lib/gcc
/root/.wine/drive_c/MinGW/libexec/gcc
/usr/bin/c89-gcc
/usr/bin/c99-gcc
/usr/bin/gcc
/usr/bin/winegcc
/usr/bin/x86_64-linux-gnu-gcc
/usr/lib/gcc
/usr/lib32/gcc
/usr/share/doc/gcc
/usr/share/doc/gcc-4.4-base/gcc
/usr/share/virtualbox/src/vboxhost/vboxdrv/math/gcc
/usr/share/virtualbox/src/vboxhost/vboxnetadp/math/gcc
/usr/share/virtualbox/src/vboxhost/vboxnetflt/math/gcc
/usr/share/virtualbox/src/vboxhost/vboxpci/math/gcc
/var/lib/dkms/vboxhost/4.1.0/build/vboxdrv/math/gcc
/var/lib/dkms/vboxhost/4.1.0/build/vboxnetadp/math/gcc
/var/lib/dkms/vboxhost/4.1.0/build/vboxnetflt/math/gcc
/var/lib/dkms/vboxhost/4.1.0/build/vboxpci/math/gcc

This means find the file that ends with gcc, you can further trim the search using grep:

locate -r gcc$ | grep bin

Another example using ignore case option:

Linux commands are case sensitive, sometime you are not sure about the case but you know the name of the file, you can do this.

locate -i reaver | grep bin

result:

/usr/bin/reaver
/usr/local/bin/reaver

Notice the difference

root@bt:/# locate -r ‘/bin/gcc$’
/usr/bin/gcc

This search is only interested the file that ends at gcc.

root@bt:/# locate -r ‘/bin/gcc’
/root/.wine/drive_c/MinGW/bin/gcc.exe
/root/.wine/drive_c/MinGW/bin/gccbug
/usr/bin/gcc
/usr/bin/gcc-4.4
/usr/bin/gccmakedep

This search matches any file that contains gcc.


Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s