Section 2.6. I Need to Search Within a Bunch of Files


2.6. I Need to Search Within a Bunch of Files

The grep commands work well in Linux because most Linux files are text files. That means grep can read through most Linux files of your choice and display the lines with patterns that match the search terms of your choice.

Naturally, the grep command, by itself, is suitable only for advanced users. However, the GNOME and KDE file search tools can serve as GUI "frontends" to the grep command for regular users.

2.6.1. The grep Command

There are several related commands that are simply subsets of grep. For example, egrep is the same as grep -e, fgrep is the same as grep -f, and rgrep is the same as grep -r.

The GUI tools described in the previous annoyance are also, in part, front-ends to the grep command. You can use them to search through the files in the directories you need with the search term of your choice.

This section does not really address an annoyance but provides a detailed description of the grep commands. Understanding grep is an important skill to addressing other annoyances.

The simplest version of the grep command searches within the current directory. For example, the following command searches for all instances of the word "Linux" within the current directory:

 grep Linux * 

Unfortunately, this command may do less than you expect. It does not search in hidden files, nor through files in subdirectories. But the output does at least identify the file that contains the search term; for example, the output from this command in my home directory starts with:

 acroread: Linux) 

which tells me that the search term Linux exists in the acroread file. If you want to search hidden files in the current directory, use the expression and wildcard associated with hidden files. In this case, it would be:

 grep Linux .* 

If you want to search in subdirectories, you can search recursively. That's the function of the rgrep command, which you can also run as grep -r:

 grep -r Linux . 

Note that I use the dot to represent the current directory; this command starts in the current directory and then searches files in subdirectories.

The rgrep command is available only for current versions of Debian Linux.


Naturally, you can use multiple words in the search term; all you need are quotes. For example, I can find all instances of "Red Hat" in my directories with the following command (I add the -s switch to suppress error messages; otherwise, you'll see a bunch when grep tries to search inside a directory as if it were a file):

 grep -rs 'Red Hat' . 

Many other searches are possible; for more information, see the manpages associated with the grep command. For some of the more important grep switches, see Table 2-5.

Table 2-5. Some important grep switches

Switch

Description

-a

Includes binary files in the grep search.

-c

Substitutes the number of lines in each file that match the search term for regular grep output.

-e

Equivalent to the egrep command; one good option if you're using a search pattern that starts with a dot.

-i

Ignores the case in the search term.

-n

Includes the line number where the search term is found in the noted files.

-o

Does not display the part of the line other than the search term.

-r

Searches recursively in subdirectories.

-s

Skips error messages.


2.6.2. Searching Within Files in GNOME

The GNOME Search for Files tool can help you find text within files. For more information on this tool, see the previous annoyance. Briefly, if you want to help your users run this tool to search within files, modify and distribute the following instructions.

It's important to emphasize to your users that searches for text within files do not work with binary files such as Adobe PDF or OpenOffice.org documents. This warning is necessary in part because Microsoft Office (for Windows and Macintosh) search tools are built to open and read the binary formats of files known to the system. However, you can emphasize that searches work well with text files such as IM transcripts.

The following steps are one example of what you can give to your users to help them search through files in their home directories:

  1. To start searching for files, click Places Search for Files. This opens the Search for Files window.

    Search for Files.

  2. Click "Show more options." In the "Contains the text" text box, enter the term you want for your search, and click Find.

    When complete, you'll see a list of files that match your search term. If the search takes too long, you can click Stop at any time.

You should modify these instructions to reflect a real search appropriate to your organization. For example, if you've configured your IM tool to save communication logs as *.im files, you could use that as a search term in the "Name contains" text box.

2.6.3. Searching Within Files in KDE

The KDE Find Files tool can also help you find text within files. For more information on this tool, see the previous annoyance. It's more capable than the GNOME tool, as it allows you to search within binary OpenOffice.org documents, spreadsheets, and presentation files.

Whether you configure the GNOME or KDE tool, it's important to emphasize to your users that tools that search for text within files do not work with binary files such as Adobe PDF documents because the tools don't understand these formats.

Briefly, if you want to help your users run this tool to search within files, modify the following instructions:

  1. To start searching for files, click the K menu Find Files. This opens the Find Files window.

    If available, enter the name of the file or wildcards associated with the files that you're looking through in the Named text box. For example, if you want to search through your OpenOffice.org Writer files, enter *.sxw or *.odt.

    If you don't know the full name of the file/directory you need, activate the "Use files index" option. This points the Find Files tool at the file database described earlier.

  2. Select the Contents tab. Enter the desired search term in the box associated with "Containing text," and click Find.

    When complete, you'll see a list of files that match your search term. If the search takes too long, you can click Stop at any time.

You should modify these instructions to reflect a real search appropriate to your organization. For example, if you've configured your IM tool to save communication logs as *.im files, you could use that as a search term under the Contents tab in the "Containing text" box.



Linux Annoyances for Geeks
Linux Annoyances for Geeks: Getting the Most Flexible System in the World Just the Way You Want It
ISBN: 0596008015
EAN: 2147483647
Year: 2004
Pages: 144
Authors: Michael Jang

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