Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-13-2009, 12:51 PM   #1 (permalink)
Osiris's Avatar
 

Join Date: Jan 2005

Location: Kentucky

Posts: 32,078

Osiris is a jewel in the roughOsiris is a jewel in the roughOsiris is a jewel in the rough

Send a message via ICQ to Osiris Send a message via AIM to Osiris Send a message via MSN to Osiris Send a message via Yahoo to Osiris Send a message via Skype™ to Osiris
Default Searching for Files in Linux via Command Line

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.)
  • <LI itxtvisited="1">c - Instead of writing the resulting names it writes the number of matches found. <LI itxtvisited="1">d - Use a different database than the default. <LI itxtvisited="1">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.)
__________________
Osiris is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fundamental Differences Between Linux and Windows Osiris Linux Tips and Tricks 0 12-11-2008 06:13 PM
Useful list of Linux websites. tribalsun Linux, BSD, other *nixes & Open Source Software 33 11-30-2008 12:35 AM
The five best things in Linux 2.6.27 Osiris Linux Tips and Tricks 2 10-21-2008 09:13 PM
New with Linux MindHacked Linux, BSD, other *nixes & Open Source Software 8 04-16-2008 09:18 PM
Want to make the move to Linux for good...advice? BoysNightOut Linux, BSD, other *nixes & Open Source Software 25 05-16-2007 11:40 AM