The other day, a co-worker was in my office, and we needed to search for a spreadsheet on the server shared drive. I (shamefully) had to boot up my VirtualBox instance of XP in order to use the gui search function in it. All the while I kept thinking, “A real nerd would know how to do this at the command prompt.”

So today I did some research. I had already heard of the tools find and locate.  And I have been using grep in different situations (like to limit results from other commands).  But I never really knew how to use these tools to find exactly what I was looking for.  Fortunately (and this is one of the best parts about using Linux) many nerds have gone before me and documented what they know.  So a simple Google search is often all it takes to step up my Linux game.

Using find

The find command has way more features than I’m about to list.  Basically, I’m just going to show you how to use it to find the location of a file based on a given parameter.  It returns the paths of any files it finds that match your search expression.

The find command uses the following syntaxt format:

find [path] [options] [expression]

Say you want to find a file named “foobar.txt”, but you can’t remember if you called it “foobar.txt” or “fubar.txt” or “foobar.odt”.  The simplest method is to use find with the -name option (or -iname to make it case-insensitive).

To search your home directory, you can use:

find /home -name *bar*

Change the path option to change the directory to search against.  This command will search the current directory:

find . -name *bar*

This one will search against the root directory:

find / -name *bar*

Using locate

The locate command works similarly to find, but it works off a database instead of actually churning through the files.  Like find, it returns the paths of any files it finds that match your expression.  Use the -i option to make the search insensitive.  locate uses the following format:

locate [options] [expression]

Now, since locate searches against its own database, you will need to update the database in order to make sure you actually locate something with locate.  Run the updatedb command as root to do so.  Ubuntu-like users can use the following command:

sudo updatedb

Once you update the database, then run the locate command.  To find our foobar.txt file, use:

locate *bar*

Using grep

The first two commands are great if you know the name (or at least part of the name) of the file you’re looking for.  But what if you want to find a file based on text within the file?  As I mentioned above, grep works wonders for limiting the results of other commands.  So if you want to ls the files that start with the letter “d”, you can use the following command (Note, that once again, the -i option makes your search case-insensitive.)

ls | grep -i ^d

In my home directory, the above command returns:

Desktop
Documents
Downloads
DVD ISOs

But I didn’t realize that I could use grep on its own, without piping the output of some other command to it.  Grep uses the following format:

grep [options] [pattern] [file]

So, if you want to search your home directory for a file that has the phrase “foo pwns bar”, you can use the following command.  Note, we are using a few more options than the usual -i for case-insensitive searching; the -l restricts the results to just a list of file names, and the -r option makes the search recursive (i.e., it drills down through the directory structure).

grep -lir "foo pwns bar" *

Since you’re searching against the text within the files, be prepared for grep to take a lot longer than the find and locate examples above which just search against the names of the files.  To make the search more optimal, try to limit it to where you think the file will be located.  The example above will search recursively through the whole directory you are in.  Feel free to change the file name to a path to optimize your search, like so:

grep -lir "foo pwns bar" /home/myfolder/Documents

And if you want grep to return the line where it found your search expression, then you can leave out the -l option.  This command:

grep -r "foo pwns bar" /home/rix/Documents/*

returns the folloing results for me:

/home/rix/Documents/foobar.txt:The power of foo pwns bar to the maxXXorz!1!!

Notice the name of the file followed by the line where it found the search expression.  Keep in mind that grep returns whole lines, so be prepared for some messy output, depending on the document.

Learn more

For more indepth tutorials, check out the following.

And check out Nixie Pixel’s video: Linux Tutorial: The Power of the Linux Find Command below

Advertisements