The grep Command

The grep Command

One of the most commonly used (and known) Unix commands is grep. Learning how to use grep will help you find files that contain a word or pattern. When the grep command is used properly, it can help you search through a file for something specific. For example, what if you wanted to search a file for a unique term?

When we talked about finding data on your Unix system, we narrowed the search down to different variables, such as the size of the file or the creation date. You can also narrow the search with grep.

You will soon learn that grep is one of the most powerful built-in programs in Unix. The program grep is also subdivided into more commands: grep, egrep, and fgrep. These three commands differ in what regular expressions they can handle. A regular expression is a pattern that can match various text strings. Regular expressions define a pattern of text that can be used to search files when a specific word or phrase to be searched for might not be known. To use grep,

Choose the word or phrase you want to find.

Find the filename you want to search.

Type grep <pattern to find> <file or files to search> at the shell prompt.

For example:

>grep "test" *.html
index.html:you passed the test

In this example, we see a quick grep of the word test in any file that resembles an HTML-based file. The grep command is a tool that will become even more valuable when you learn more about regular expressions.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows