Searching for Files in Linux via Command Line - gHacks Tech News

Searching for Files in Linux via Command Line

Being able to search for files is a fairly important aspect of using a computer. Naturally there are a number of ways to accomplish this task. You can use a powerful tool like Beagle (check out my article Enable Beagle Searching in GNOME) or you can use easier, faster command line tools to handle your searching tasks.

There are a couple of searching tools that immediately come to mind to take care of your searching needs. The first tool is locate and the second is find. I much prefer the former tool because it is much faster. But both tools can do the job. Both tools are run from the command line and can be run as either the root or a standard user. Let's take a quick look at each.

Locate

The locate command does not actually search through the directory hierarchy. What locate does is search through a database for the files you are looking for.  Because of this the search is faster. But because it uses a database that means the database has to be updated in order for new to show up in results (or deleted files to not show up in results). So before you actually start using this command you will want to update the database. In order to update the database used by locate you need to issue the command updatedb. It might be wise to set up a cron job to have the database updated regularly (as most of us won't remember to update the database as often as it is necessary.)

Once you have the database updated you are ready to search. The locate command is simple:

locate FILENAME

(Where FILENAME is the actual name of the file you are searching.

Naturally there are switches you can use with the command. The most useful of these are:

(NOTE: Switches will be proceeded by the "-" character.)

  • c - Instead of writing the resulting names it writes the number of matches found.
  • d - Use a different database than the default.
  • i - Ignore case
  • r - Use a regular expression.

The last switch should really raise the eyebrows of the hard core at heart. The locate command can use regular expressions which makes this command VERY useful.

Find

Unlike the locate command, find does search through the filesystem hierarchy. Because of this find is much slower than locate. But also because of this, there is no need to update a database. So the find command always will have the most up-to-date results.

The syntax of the find command is very similar to that of locate.

find FILENAME

Where FILENAME is the actual name of the file name to search.

And, like locate (and nearly every Linux command), there are switches to enhance the usability. The most useful of these switches are:

(NOTE: Switches will be proceeded  by the "-" character.)

  • L - Follow symbolic links
  • delete - Will delete the found files.
  • exec ; COMMAND FILENAME- Will execute the COMMAND on the FILENAME.

The find command is actually quite a bit more complex than locate. Because find's actually purpose is to examine files (the end results can be found files), find does much more. But for command line file searching, it does perform admirably.

Final Thoughts

My preferences is to use locate. I always use locate before I will use a gui search tool. I trust the results, the results are faster, and often more accurate (so long as I have updated the database.) Give either of these tools a try. You will most likely be happy with the results (unless, of course, you can't find the file you're trying to locate.)

Advertisement

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:


Previous Post: «
Next Post: »

Comments

  1. gokudomatic said on February 13, 2009 at 11:21 pm
    Reply

    and anyway, locate + grep allows to do extended search, which gui can’t.

  2. RIck Russell said on February 13, 2009 at 11:48 pm
    Reply

    Don’t forget the command ‘whereis’

    box:~$ whereis wget
    wget: /usr/bin/wget /usr/share/man/man1/wget.1.gz

    For finding the binary, source, and manual pages for any given command.

  3. Paul(us) said on February 14, 2009 at 11:29 am
    Reply

    Christal clear and very helpful as usally. Also the commented post form the reader(‘s), are very helpful.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.