File Searches


Files frequently get lost. Well, technically speaking, they don't actually get lost. We just forget where we've put them. But because of this, the shell includes some handy commands to search for files.

Using the find Command

The find command is installed on SUSE Linux by default. Like the Search option on the Windows XP Start menu, this command manually searches through all the files on the hard disk in order to find what you're looking for. It's not a particularly fast way of finding a file, but it is reliable.

Here's an example:

find /home/knthomas -name "myfile"

This will search for myfile using /home/knthomas as a starting point (which is to say that it will search all directories within /home/knthomas, and any directories within those directories, and so on, because it's recursive).

You can search the entire file system by leaving out the initial path. In this case, find will assume you want the search to start from /, the root of the file system.

If the file is found, you'll see it appear in the output of the command. The path will be shown next to the filename.

Period punctuation symbols have interesting meanings within file listings and therefore within the output of the find command. As you learned in Chapter 14, .. refers to the parent directory of the one you're currently browsing. In a similar way, a single . refers to the directory you're in at the moment; it's shorthand for "right here." So, if find returns a result like ./myfile, it means that myfile is right here in the current directory. However, when a single period is used at the beginning of a filename, such as in .bashrc, it has the effect of hiding the file. In other words, that file won't appear when you type ls (although you can type ls -a to see all files, even those that are hidden).

If you give find a try, you'll see that it's not a particularly good way of searching. Apart from being slow, it will also return a lot of error messages about directories it cannot search. This is because, when you run the find command, it takes on your user permissions. Whenever find comes across a directory it cannot access, it will report it to you, as shown in the example in

Figure 15-5. There are frequently so many of these warnings that the output can hide the instances where find actually locates the file in question!

image from book
Figure 15-5. The find command is useful for finding files but isn't problem-free.

There are various ways around avoiding error messages, but perhaps the quickest solution is to use the su command to switch to root user before using the find command. Because the root user has access to every file on the hard disk, the find command will be unrestricted in where it can search, so it won't run into any directories it doesn't have permission to enter.

However, an even better solution for finding files is to use the locate command.

Using the locate Command

Although locate comes as standard on most versions of Linux, it's not installed on SUSE Linux by default. You can easily install it with two steps:

  1. Insert your SUSE Linux installation DVD-ROM. To install the software automatically via a command-line version of the YaST2 tool, type the following at the shell:

    yast2 -i /media/dvd/suse/i586/findutils-locate-4.1.7-860.i586.rpm

  2. Next, you need to update the locate database. Switch to root user and type the following command:

    updatedb

    This will probably take a minute or two to complete.

After this, you can use the following command to search for a file (you don't need to be logged in as root to do so):

locate myfile 

The benefit of using the locate command is that it's instantaneous. Instead of searching the file system, a database is searched instead. When locate installs, it adds a daemon to your system, which means that the database is periodically and automatically updated in the background (usually once a day).

Note 

The downside of using the locate command is that it relies on the database being up-to-date. If you save a file, and then an hour later use locate to find it, the chances are the database won't have been updated during that period. In such a case, the find command is a better bet.

Using the whereis Command

There's one other command worth mentioning in the context of searching: whereis. This locates where programs are stored and is an excellent way of exploring your system. Using it is simply a matter of typing something like this:

whereis cp

This will tell you where the cp program is located on your hard disk. It will also tell you were its source code and man page are located (if applicable). However, the first path returned by the search will be the location of the program itself.




Beginning SUSE Linux from Novice to Professional
Beginning SUSE Linux: From Novice to Professional
ISBN: 1590594584
EAN: 2147483647
Year: 2005
Pages: 293
Authors: Keir Thomas

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net