Linux grep command

In very simple terms, if you want to search anything inside a file then you use grep command.

Note: Linux grep is different from Linux find command. Linux find command is used for searching files and directories. Linux grep is powerful and can be used for searching a pattern inside a file.

Sometime or the other you must have encountered the situation of searching something inside a file. This is the perfect situation to use Linux grep command as it comes handy with every Linux distribution.

Grep syntax

grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.

Search inside file

Grep searches for lines containing a specified pattern and, by default, writes them to the standard output. Grep is a case sensitive command:

grep oracle /tmp/installer.txt

The above command will print each line containing the word oracle.

grep Oracle /tmp/installer.txt

The above command will print each line containing the word Oracle.

Grep -i

If you want grep to ignore the case of the search term, then you use -i

grep -i oracle /tmp/installer.txt

Search phrase

To search a specific phrase inside a file, you must enclose the phrase inside single quotes. 

grep -i 'oracle database' /tmp/installer.txt

The output will contain all the lines which has oracle database in it.

Count total matches

Sometimes you just want to count all the lines that contain a specific word. In this case you must use -c option with grep command.

grep -c oracle /tmp/installer.txt

The output will be total number of files that contain the word oracle. It will just print the total number.

Get line number

The -n option of the grep command prints the line number as the output. This helps is easy debugging of results and know the exact line number inside the file.

grep -n oracle /tmp/installer.txt

The above command will print line number along the exact line that contains the word oracle.

Display files containing string

We can use grep command to get the name of the files that contain a specific string inside. 

grep -l oracle file1.txt file2.txt file3.txt file4.txt

Output: file2.txt

Inverting pattern match

You can display the lines that are not matched with the specified search sting pattern using the -v option.

grep -v oracle /tmp/installer.txt

The output will be all the lines that do not contain the word oracle.

Lines that start with a string

The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.

grep '^oracle' /tmp/installer.txt

The output will be all the lines that start with oracle word.

Lines that end with a string

The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.

grep 'oracle$' /tmp/installer.txt

The output will be all the lines that end with oracle word.

Was this article helpful?

Related Articles

Leave a Comment