Just a thought

Search commands in Linux (Ubuntu)

Posted by stringofthoughts on April 5, 2009

File searching is one of the very important task and also very frequent task.  Fortunately for us there are few very awesome shell commands available to achieve this. locate, find and grep. locate has improved implementation in different distributions like slocate, mlocate and rlocate.


locate command doesn’t really search the disk for your query. It search in a database which keeps the PATH info of the files in the system.  whis is usally available in /var/lib/slocate/slocate.db or /var/lib/mlocate/mlocate.db . This database is automatically updated on a regular basis by cron, a small program that runs in the background, performing various tasks at regularly scheduled intervals.  You can update the database manually by issuing the command

$ sudo updatedb

The problem with locate is that it could give you wrong results because database is not updated in real time. Files added to and deleted from the system after database update will not show up in locate query. so in this sense this is not very good tool.


slocate is security enhanced version of locate. It works the same way as locate except it also stores the file permission in the database. so when a user calls it I’ll only show the file in query user has permission to access. This is good feature for a multiple user system.


mlocate is another implementation of locate. mlocate is quite similar to slocate in the security sense the difference comes in updating the database. The m stands for merging. mlocate keeps the timestamp on the files and while updating database it uses the existing database. This makes update much faster and less demanding on HDD. This feature is only available in mlocate.


rlocate is the ultimate search tool. It works the same way as locate work, it uses a database but the awesome thing about this database is that this database is updated in real time. What does that mean? Well it means if you delete, add, copy of move a file inside the system the database will be immediately updated. It achieves it using is kernel module that intercepts all paths modifications and a daemon that logs those operations on a differences file

Locate usage:

$ xlocate <options> <file_name(s)> // x = s/m/r

It also displays the files which contains the queried string. let’s say you are searching for a file “task” and the system has other file like “task1, task2, your_task etc” it’ll print each of these file.


$ find <directory_path> -name <file_name>

$ find / -name msp430 // searches system for file with string msp430

$ find /home/geek/ -name msp430 // searches /home/geek/ for file with string msp430

Find works very straight forward way. It searches the whole <directory_path> one file at a time. so it’s slow but it’ll never give you wrong result like locate/slocate/mlocate could. It’s little slow but works fine for me.


Grep is awesome and also one of the reason i love Linux. Grep is a command line text search utility. It’s very useful for example you use the simple command ls quite often but let’s say the directory contains hundreds of files then you’ll have a hard time looking through the list. This is where grep comes in. Let’s say you want to search for files which contains the name JAVA in it.  here is what you ‘ll do

$ ls | grep -i java

Pipe [ | ] combines commands in Linux shell.  ls generates the list it gives it to grep and grep looks for the word java in each line and prints only lines that contains java. -i makes the search case insensitive (grep is case sensitive by default) let’s say checking your nVIDIA graphics card model number (in case you forgot or you are using a different machine).

$lspci | grep -i nvidia  // lspci displays pci bus info and devices

So these are the useful search commands in Linux. New Linux users start using shell more often. It’s confusing at first but it’s the most powerful tool.


One Response to “Search commands in Linux (Ubuntu)”

  1. Hi. I treasured to drop you a quick note to express my thanks. I’ve been observing your blog for a month or so and have plucked up a heap of sound information as well as enjoyed the way you’ve structured your site. I am setting about to run my own blog however I think its too general and I would like to focus more on smaller topics.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: