How to Use grep Command in Linux with Examples


Use grep Command in Linux

The grep command used to find a particular string or pattern in one or multiple files. Grep stands for “Global Regular Expression Print”. Grep is the most powerful command in Linux used most commonly. It can be also used to read Standard Output from another command. In this tutorial, you are going to learn how to use grep command in Linux.

Basic Syntax for grep Command

Following is the basic syntax for the command grep.

grep [OPTIONS] PATTERN [FILES]

OPTIONS: Grep options are used to change the behavior of the command.

  • -i: Used for ignoring matching case.
  • -c: It prints the count of lines matching.
  • -l: Displays a list of filenames.
  • -n: Number of matched lines and their line numbers.
  • -v: Number of lines that do not match the pattern.
  • -w: Match the whole word

PATTERN: Provide a search pattern.
FILE: Input file names.

Grep command to find a particular string inside the file

Grep command mostly used for searching string in the file. You can also search string inside files case-sensitively.

Run the command below to search a string John inside names.txt file:

grep John names.txt

If you want to search John case-insensitively then run the grep command with option -i .

grep -i John names.txt

If the string you want to search includes spaces then enclose them inside single or double-quotes. In the below example, it will search John Hanks in names.txt file.

grep -i "John Hanks" names.txt

Display Number Of Matching Lines

You can display a number of matching lines using grep command with -c option. Run the following command to find the count of the string John in names.txt file:

grep -c John names.txt

Invert Match in Grep Command

Invert match the (-v) option is used to invert the grep output. The command will display the lines which do not match the given pattern.

To display the lines which do not match string John inside names.txt file run following command.

grep -v John names.txt

Recursive Search

To search given string inside all the files inside a directory -r –recursive option is used.

In below example string “John” will be searched in all files inside colleges directory:

grep -r John /Documents/colleges

By using option -R you can also search symbolic link files inside directories:

grep -R John /Documents/colleges

To search string in all directories you can run the following command:

grep -r "John" *

Search String in Standard Output

If you want to search string inside a command output, you can do so by combining grep command with another command or commands.

To find the string inet6 inside command output of ifconfig run following command:

ifconfig | grep inet6

Search Exact Matching Words

You can search exact matching words inside the file by using the option-w with grep command.

By using the following command you will only get lines which contain the exact word John inside the file:

grep -w John names.txt

Show Number of Lines Matching

To display the count of lines matching inside a file you can use the option -c with grep command.

For example, You can get the count of lines containing string John by running the following command.

grep -c John names.txt

You can also get the count of matching lines with standard output to do so run the following command.

ifconfig | grep -c inet

Above command will search string inet inside standard output provided by the command ifconfig.

Conclusion

You have successfully learned how to use grep command in Linux. If you have any queries regarding this then please don’t forget to comment below.