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.
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:
(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.
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.
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.
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
and anyway, locate + grep allows to do extended search, which gui can’t.
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.
Christal clear and very helpful as usally. Also the commented post form the reader(‘s), are very helpful.