Next 14.H: Sort with sort Up 14: File Utilities Prev 14.F: Find with find Contents


Specialized

§ 14.G: Search Files with the grep Family


The grep command (and its relatives egrep for expression grep and fgrep for fast grep) are great for finding some specific information in a file, or for finding which files contain some specific information. For example, to search all files in the present directory for the word Jack

Here * is a wildcard which gets replaced by all file names. Unix will then show you the names of the files containing the word Jack, and the lines containing the word:

So now you know both what is said about Jack, and where it is said.

Like most Unix commands, the greps are case sensitive. For example,

may well find nothing. If you want grep to be case insensitive, you can use the option -i to make it ignore the case:

Be warned, the grep family may have some difficulty searching for phrases containing punctuation and special characters. For example, grep will interpret > to be an output redirection character and not part of your search string. On some systems, enclosing your string in single quotes (grep 'answer >' *.dat) will work.

Check your man pages and the references for the proper syntax for each of the greps and for specialized searches.


Next 14.H: Sort with sort Up 14: File Utilities Prev 14.F: Find with find Contents