If you are looking for a string in one or more text
files, use 'grep'. 'grep' searches texts for a specific pattern.
Let's say you are searching for files in '/usr/src/linux/Documentation' which
contain the string 'magic':
$ grep magic /usr/src/linux/Documentation/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?
The file 'sysrq.txt', among others, contains this string
in the context of discussing the SysRQ feature.
By default, 'grep' only searches the current directory.
If this directory contains many subdirectories, 'grep' will list them with
lines like these:
grep: sound: Is a directory
This can make 'grep's' output quite a nuisance to read.
There are two ways to prevent 'grep' from doing this:
- telling it to search the subdirectories, too: grep
-r
- or telling it to skip directories: grep -d skip
If you are expecting a lot of output, you should send
it to program like 'less' using a pipe:
$ grep magic /usr/src/linux/Documentation/* | less
Now you can scroll the output conveniently.
An important thing to remember is that you have to provide
a file filter (just add * for all files). If you forget this, 'grep' will
just wait and thus block the terminal. If this happens to you, press <CTRL
c> and try again.
Some of the more interesting command line options for
'grep':
- grep -i pattern files makes
the search case-insensitive. The default is case-sensitive search.
- grep -l pattern files just
lists the names of matching files.
- grep -L pattern files lists
the names ofnon-matching files.
- grep -w pattern files only
matches whole words, not substrings ('magic' vs. 'magical').
- grep -C number pattern files
determines how many [number] lines of context are displayed around a hit.
- grep pattern1 | pattern2 files
lists lines which contain either pattern1 or pattern2.
- grep pattern1 files | grep pattern2
lists lines which contain pattern1 together with pattern2.
There are also some special signs which help you to
refine your search:
- \< and \> match the beginning
and the ending of a word.
Examples:
- grep man * finds 'Batman', 'manic',
'man' etc.
- grep '\<man' * finds 'manic' and
'man', but not 'Batman'.
- grep 'man\>' * finds 'man' and
'Batman', but not 'manic'.
- grep '\<man\>' only finds 'man',
not 'Batman' or 'manic' or any other string.
- '^' : means 'only words with the following string
which are at the beginning of a line'
- '$' : means 'only words with the proceeding string
which are at the end of a line'
-
If you can't be bothered with command line options,
there's also a graphical interface to 'grep', called reXgrep.
It offers a search engine like AND, OR, NOT syntax and some nice buttons
:-). If you just need a clearer output, get fungrep.
section index top
-
pinfo:
'pinfo' runs in an terminal and displays man and info pages. It searches
for an appropriate info page first and then for a corresponding man page.
If it doesn't find either of them, it calls 'apropos'.
References are provided as links, and the output is nicely colorized. You
can customize colorization. 'pinfo' also adds the much needed search function
to 'info'. Included on the Mandrake Linux CDs.
-
KDE Help. If you happen to use KDE, you can use
KDE's own help browser to search and display man and info pages. Note the
syntax:man:program_name, butinfo:(program_name).
-
Gnome Help Browser. The central help tool for the
GNOME environment, which displays almost everything under the sun. The syntax
for man and info pages is the same: man:program_name
or info:program name.
-
HTML Browsers. HTML is an increasingly popular help
file format. It is much easier to write pages in than using 'man' or 'info'
macros. You can bookmark the appropriate pages in your browser of choice,
and use the browser's built-in search function. To search HTML pages with
'grep', use lynx' 'dump' option:
lynx -dump file1 file2 | grep pattern
-
'TkInfo', 'TkMan', 'Xinfo', 'Xman'. X programs to
display and search 'man' or 'info' pages.
section index top
|