WebMasterCampus
WEB DEVELOPER Resources

Linux grep Command

Learn Linux grep Command with examples


Linux grep Command

In Linux we use “grep” command used to search for a string of characters in a specified file. The grep command is useful when searching through large log files.

The text search pattern is called a regular expression. When it finds a match, it prints the line with the result.

grep Command Syntax

~$  grep "String to Search" File_name_where_search

Three parts of grep command.

  • The first part starts with grep word.
  • Second the pattern that you are searching for.
  • Third the file name that the grep searches through.

grep Command Search Example

Let’s search google.com string in web_browswers_name.txt file.

~$  grep google.com web_browswers_name.txt

grep Command Search Multiple Files Example

~$  grep google.com result_1.txt result_2.txt result_3.txt

grep * to Search All Files in Directory

We can use an asterisk instead of a filename at the end of a grep command to search all files in the current directory.

~$  grep google *

grep -w to Find Whole Words Only

grep -w use to search for the word google in all files in the current directory, append -w to the grep command.

grep -w option only prints the lines with whole-word matches and the names of the files it found them in.

~$  grep -w google *

Without -w option, grep displays the search pattern even if it is a substring of another word.

grep -i to Ignore Case in Searches

grep command is case sensitive, to make it case insensitive we can use -i option. The terminal displays both uppercase and lowercase results and the output includes lines with mixed case entries.

~$  grep -i google *

grep -r to Search Subdirectories

grep -r is use to to search into all the subdirectories.

~$  grep -r google *

You can use grep to print all lines that do not match a specific pattern of characters. To invert the search, append -v to a grep command.

To exclude all lines that contain phoenix, enter:

~$  grep -v google sample

grep -x To Show Lines That Exactly Match a Search String

The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option.

~$  grep -x google number3” *

grep -l To List Names of Matching Files

Use grep -l to print only the filenames that match your search.

~$  grep -l google *

grep -c to Count the Number of Matches

Use grep -c operator to count the number of matches as well. Grep can display the filenames and the count of lines where it finds a match for your word.

~$  grep -c phoenix *

grep -n to Display Line Numbers with grep Matches

When grep prints results with many matches, it comes handy to see the line numbers. Append the -n operator to any grep command to show the line numbers.

We will search for Phoenix in the current directory, show two lines before and after the matches along with their line numbers.

~$  grep -n -C 2 Phoenix sample

grep -m2 Limit grep Output to a Fixed Number of Lines

Individual files, such as log files, can contain many matches for grep search patterns. Limit the number of lines in the grep output by adding the -m option and a number to the command.

~$  grep -m2 Phoenix sample

grep -A to add number of lines to display after a match

Use -A and a number of lines to display after a match: grep -A 3 phoenix sample - this command prints three lines after the match.

grep -B to add number of lines to display before a match

Use -B and a number of lines to display before a match: grep -B 2 phoenix sample - this command prints two lines before the match.

grep -C to add number of lines to display before and after the match

Use -C and a number of lines to display before and after the match: grep -C 2 phoenix sample - this command prints two lines before and after the match.

Created with love and passion.