Searching Files and Directories Quickly


You've learned how to list files with the ls command and change your working directory with the cd command. To find a specific file using only ls and cd, however, you must navigate slowly through directory trees, listing each directory, directories inside those directories, and so on, until you find the file you want. Searching with ls and cd is fine for a collection of two or three files, but how about a collection of a hundred files? A thousand? Eventually, all those filenames will begin to look the same.

Rather than search all day by hand, you can use the find and locate commands to find the location of your stray files more quickly.

Searching for Files with find

You can use the find command to search an entire directory tree or list of directory trees for specific filenames or for filenames that match a specific pattern. The syntax for using find this way is

 find tree1 [tree2 ...] -name filename -print 

Calling find this way searches the supplied directory trees for a specific filename. For example, you can search your home directory for myfirstfile.txt:

 [you@workstation20 firstfiles]$ find ~ -name myfirstfile.txt -print /home/you/firstfiles/myfirstfile.txt [you@workstation20 firstfiles]$ 

The Tilde Character Takes You Home

Remember that the tilde character (~) represents the equivalent of /home/you to the shell.


The find command quickly locates myfirstfile.txt for you. It is stored in /home/you/firstfiles. Similarly, you can search your home directory for all files ending in .txt by supplying a pattern to find similar to the kind of pattern used for filename expansion. You need to quote the pattern you supply, however, or the shell will match the pattern to the text files in your home directory before find gets to see it:

 [you@workstation20 firstfiles]$ find /home/you name '*.txt' -print /home/you/green/color.txt /home/you/anotherfile.txt /home/you/BusinessPlan.txt /home/you/evolution/RDF-urls.txt /home/you/firstfiles/myfirstfile.txt /home/you/firstfiles/mysecondfile.txt /home/you/firstfiles/mythirdfile.txt /home/you/illustration.txt [you@workstation20 firstfiles]$ 

The find command has searched your home directory and found three files whose names end with .txt. The full paths to these files have been printed for you. (Don't worry if your list of files doesn't look exactly the same; the list of files you see depends on what files actually exist on your computer!)

Searching the Entire File System with locate

Sometimes you want to search the entire Linux file system for a specific file. When you want to search the entire file system, locate is often a better choice than find because locate is able to search through a much longer list of files much more quickly. You already know how to use find to run a file-systemwide search; suppose you want to find all the JPEG pictures in your Linux file system. You can do so by issuing a command like

 find / -name '*.jpg' 

However, you would soon find that this kind of a search can be time-consuming because find crawls to every corner of your hard drive, directory by directory, looking for filenames that end with .jpg. You would also find your screen filling with error messages as find encountered place after place in the Linux file system that you, logged in as a normal user, don't have permission to access. (For a refresher on permissions, return to Chapter 7, "Understanding File Properties.")

For large filename searches across the entire Linux file system, the locate command can often be more efficient because locate consults a large database that indexes every file in your Linux system. Using locate is simplejust supply the text you want to search for as an argument:

 [you@workstation20 firstfiles]$ locate '*.jpg' /usr/lib/pygtk/2.0/demos/images/background.jpg /usr/lib/mozilla-1.7.6/res/samples/bg.jpg /usr/lib/mozilla-1.7.6/res/samples/raptor.jpg /usr/lib/openoffice.org1.9.83/share/gallery/www-back/structure_green.jpg /usr/lib/openoffice.org1.9.83/share/gallery/www-back/lino-green.jpg [...] /usr/share/nautilus/patterns/chalk.jpg /usr/share/nautilus/patterns/burlap.jpg /usr/share/emacs/site-lisp/emacspeak/etc/emacspeak.jpg /usr/share/gtk-2.0/demo/background.jpg /usr/share/pixmaps/gnect/bg_nightfall.jpg you@workstation20 firstfiles]$ 

You Can Pause the Output of Commands

The output of the locate command will likely scroll off your screen. You can pause the output of locate or other commands by appending the text |more or |less to the end of the command:

 locate '*.jpg' |more 

This command displays the output of the locate command one page at a time, waiting after each screenful of information for you to press the spacebar before continuing. It passes the output from the locate command to a pager like less so that you can read it in chunks.

This technique, known as piping, is discussed later this chapter, in the section titled "Using Pipes to Link Commands."


Is locate Giving You Error Messages?

If the locate command tells you that it is unable to open the database, your system hasn't yet been indexed by the nightly Linux indexing process. To rectify this situation, either wait a day before trying locate again or log out, log back in using the root account, and run the updatedb command without arguments at a shell prompt before trying locate again.


Although the actual list of image files you see will vary depending on whether you customized your Fedora Core 4 installation in Chapter 2, "Installing Fedora Core 4," and what sorts of files you've created in your home directory since then, you can receive a long list of .jpg files via locate almost instantly.

The database used by locate is rebuilt once daily when the system runs the updatedb command. The data displayed by locate might at times not be current with respect to the work you've done in the past few hours. However, it enables locate to find large numbers of files across the entire disk rapidly.

Saving a List of Found Files

Sometimes saving the output of a search command is helpful. The shell can help you save the output of commands like find and locate by redirecting their output to a file. You can then load such a text file into any editor for editing, perusal, or printing. To redirect the standard output of a command (the information it prints to the console to fulfill your request), use the greater-than symbol (>), followed by the name of the destination file. Return to your home directory and save the list of JPEG files on your system to a file called myjpegs.txt:

 [you@workstation20 firstfiles]$ cd [you@workstation20 ~]$ locate '*.jpg' > myjpegs.txt [you@workstation20 ~]$ ls l myjpegs.txt -rw-rw-r--  1 you   you    25214 Mar 25 21:12 myjpegs.txt [you@workstation20 ~]$ 

As you can see, there is now a text file of decent size in your home directory called myjpegs.txt. This file contains the list of JPEG files in your Linux file system, as reported by the locate command. If you want to view the file, feel free to read it with a pager like less or to load it into a text editor.

You can redirect the output of nearly every command available at the command prompt. It is also possible to append the standard output of a command to an existing file by using a double greater-than symbol (>>). For example, to add a list of all GIF images on your system to myjpegs.txt, use

 [you@workstation20 ~]$ locate '*.gif' >> myjpegs.txt [you@workstation20 ~]$ 

Be Careful When Redirecting Output

To avoid accidentally overwriting important contents, always be cautious when appending command output to an existing file. When you use the single greater-than symbol (>) to redirect the output of a command to a file that already exists, the content of the original file is overwritten by the new data. You therefore must be careful to use double greater-than symbols (>>) when redirecting data so that you don't overwrite important files.


Searching Text Files for Word Patterns

You've now created a rather lengthy text file in your home directory called myjpegs.txt, which contains a list of all the JPEG images and all the GIF images stored in your Linux file system. But suppose you want to narrow down your list a bit further: Suppose you want to find only pictures of outer space.

You could load myjpegs.txt into a text editor or browse it with a pager to visually search for files that match your criteria, but you can find what you want more quickly by using the grep command to search myjpegs.txt. The grep command's sole purpose is to search text files for words or patterns that you supply. You call the grep command like this:

 grep pattern file1 [file2 ...] 

Here, pattern is the text string or pattern to search for, and file1, file2, and so on are the files that grep is to search. Lines from these files that contain the text string or match the pattern will be displayed on standard output (that is, to the console). Try searching for pictures of space now:

 [you@workstation20 ~]$ grep space myjpegs.txt /usr/share/nautilus/patterns/chalk.jpg /usr/share/nautilus/patterns/burlap.jpg /usr/share/emacs/site-lisp/emacspeak/etc/emacspeak.jpg /usr/share/gtk-2.0/demo/background.jpg /usr/share/pixmaps/gnect/bg_nightfall.jpg /usr/share/pixmaps/gnect/bg_grotty.jpg [...] /usr/share/backgrounds/images/space/hst_antennae_9734a.jpg /usr/share/backgrounds/images/space/hst_mars060.jpg /usr/share/backgrounds/images/space/apollo08_earthrise.jpg /usr/share/backgrounds/images/earth_from_space.jpg /usr/lib/python2.4/site-packages/Ft/Share/Demos/images/Cards/Cribbage/ spacer-border.gif /usr/share/doc/openjade-1.3.2/jadedoc/images/space.gif [you@workstation20 ~]$ 

The grep command has found a number of pathnames in myjpegs.txt that contain the word space. Used this way, grep is a great tool for mining data from long lists.

You Can Use grep to Find Files, Too

You can also use grep to help you find files that contain a certain word. When you search for a word using grep and the -l argument, grep simply prints the names of the files that contain the word in question. For example, if you want to know which files in your home directory contain the text jpg, you can issue the following command to search for jpg in all files in your home directory:

 [you@workstation20 ~]$ grep -l jpg ~/* myjpegs.txt [you@workstation20 ~]$ 


Be Careful About Capitalization

When you use grep, you perform a case-sensitive search; the capitalization of the word you're searching for must mirror the capitalization of your search term so that grep can find a match.

To cause grep to perform a case-insensitive search, use the -i argument:

 grep -il jpg ~/* 




    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

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