Learn how To Use grep Command.

Created:

Introduction

The grep command is a great tool you must learn if you want to be a good software developer. The command is used to search text inside files.

Grep is a command that is available in most of operationg system (except on Windows). Here we are going to explain how to use the grep command with several examples.

Once you learn how to use grep you will find that it will help you a lot during your work.

We separate the turorial in two parts: * Basic usage: the minimal options you need to know to use grep. * Advance usage: It will help you with edge cases.

Grep basic usage

Command parameters or how to use it

The command is very easy to understand. In the next example we search for "tutorials" inside the file example.txt:

    grep 'tutorials' example.txt

You can also use pipes to send the output to grep:

    cat example.txt | grep 'tutorials'
    ps ax | grep 'python'

The first command is equivalent to grep 'tutorials' example.txt. The second command allows you te filter running program by their name, in this case we filtered all python running program using grep and ps command.

As another example using pipe we will display cpu model name with:

cat /proc/cpuinfo | grep -i 'Model'

Grep can also ignore word case using the optipns -i like in the next example:

grep -i "boo" example.txt

Using -i will match with words like boo, Boo, BOO, BoO, etc.

Using grep to search recursively

If you need to search inside a directory you need to search recursively i.e. read all files under each directory.

$ grep -r "tutorials" /tmp/

The command above will search "tutorials" in the whole directory tree from /tmp.

To include the filename use the option -h.

Grep advanced usage

The search field of the grep command is actually a regular expression, remember this for the following advance usages.

Searching words only

As you noticed with basic usage is that when search for a string sometimes it matches more thn you want. You can use grep to serch for word using the option -w.

grep -w "tutorials" example.txt

If the file conatains the word tutorials it will return it, but only if the word matches.

Using grep to search 2 different words

Use the egrep command as follows:

grep -w 'tutorial\|technology' example.txt

if the command does not work for you try to use egrep:

egrep -w 'tutorial|technology' example.txt

The command above will match for tutorial or technology if found inside example.txt.

Force grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:

grep -v bar /path/to/file

Using wilcard to searhc in files

To search for void in all c files:

grep -l 'void' *.c

The command above will return all the filenames that has void inside, grep will only serch in .c files.

Appendix

  • -l option to list file name whose contents has the matching regex.
  • -n option will print the number of the line in the text file of the matched regex.

More options

Why the command is called grep?

The editor ed there is a command to perform a text search inside files. The command is:

g/re/p