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:
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.
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.
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:
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:
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.