grep Command Tutorial In Linux with Examples

The grep command is the very popular command used to filter string, text, and data in files and command outputs. From an academic point of view, the grep command searches for PATTERNS in each FILE. PATTERNS can be simple text, number, string, or regex (regular expressions).

grep Command Syntax

The grep command has the following syntax.

grep TERMS FILE
  • TERMS is single or multiple terms which are searched in the specified FILE or redirected input.
  • FILE is single file or directory where all files will be searched for the TERM.

Display Help and Option Information

The grep command provides a lot of options and help information about these options can be displayed with the --help option like below.

$ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -F, --fixed-strings       PATTERNS are strings
  -G, --basic-regexp        PATTERNS are basic regular expressions
  -P, --perl-regexp         PATTERNS are Perl regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions in patterns and data
      --no-ignore-case      do not ignore case distinctions (default)
  -w, --word-regexp         match only whole words
  -x, --line-regexp         match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages

Search Text

A specified text can be searched in the specified file. In the following example, we search “ale” in the text file “MyFile.csv“.

$ grep ale MyFile.csv
Search Text

In the output, we can see that the provided term is highlighted for each match. Unmatched lines are not displayed.

We can also specify the file we want to search as an absolute or full path like below.

$ grep ale /home/ismail/MyFile.csv

Grep Multiple Files

Grep command provides useful options where searching for multiple files is one of them. The files we want to search can be specified after their search term by separating their spaces.

$ grep ale MyFile.csv YourFile.csv AllFiles.csv

Search All Files In Specified Directory

another useful feature of the grep command is searching all files in the specified directory. The bash glob operator can be used to specify all files at the specified directory. In the following example, we search the term “ismail” in all files in the current working directory.

$ grep ismail *

Alternatively, we can specify another directory providing its absolute or full path like below.

$ grep ismail /home/ismail/Downloads

Search Whole Word

By default the grep command searches and matches specified terms in some part of the line or word. We can search and match the whole word which is separated with spaces by using the -w option.

$ grep -w pale *

Search CaseInsensitive

By default the grep command searches and matches case-sensitive manner. This means the uppercase and lowercase letter is interpreted as different and not matched. We can disable sensitivity for search and make a case-insensitive search with the grep command. The -i option is used for case-insensitive search.

$ grep -i PaL MyFile.csv
Search CaseInsensitive

Recursive Search (Search Subdirectories)

The grep command can be also used to search directories and their subdirectories in a recursive manner. The -r option is used to search the specified path with all of its subdirectories.

$ grep -r ismail /etc/

Inverse Search (List Unmatched Lines)

By default the grep command searches and displays matches lines. But it can be also used for inverse search where the unmatched lines can be listed too. The -v option is used for inverse search.

$ grep -v ismail /etc/passwd

Match Whole Line

Another match option for the grep command is matching the whole line. If the specified term does not match as a whole it is not displayed. The -x option is used to match the whole line.

$ grep -x "I am a whole line match" /home/ismail/*

Display Name Of Matched Files

By default only matched lines are displayed by grep. There are no details about the match. But using the -l option the matched file names can be displayed for each match.

$ grep -l "ale" /home/ismail/*
Display Name Of Matched Files

Count Number of Matches

Another great feature provided by the grep command is counting the number of matches. This prints the count number of matches for every file. the -c option is used to count matches.

$ grep -c "ismail" /etc/*

Leave a Comment