How to use grep command in Linux with examples

Grep command is used to search text or string in any text file. Grep command in Linux stands for  “global search for the regular expression”. The text for which search is executed using Grep command is called regular expression.

Syntax

grep <options> <pattern> /path/to/file

Example

grep -i “infojinx” /home/test.txt

This command searches for word infojinx in test.txt file in home folder. Use -i makes search ignore case sensitivity.

grep command in Linux has multiple options for search few of them are as below

OptionsDescription
-isearches ignoring case
-lsearch displays name of file matching pattern
-nsearches and displays matching line with its number
-csearches and displays only count of lines where pattern matches 
-vinverse match i.e. display line which does not match pattern searched for
-wMatches word i.e. matches string even if it exists as substring
-hDisplays on matched line and not file name

Linux – Grep command with examples

Search for file using grep command

Grep command can be used to search file in a folder using below syntax

Syntax

grep file_name /path/for/search

Example

grep test.txt /home

To ignore case sensitivity during file search use switch -i

grep -i test.txt /home

Search for string in a file using grep command

Grep command can also be used to search any string in file or files. Note: grep will only search for string given if it exist as word and ignore this string if its part / substring of a word

Syntax

grep “string_to_search” file_name

Example

grep “infojinx” test.txt

The above command will search for word infojinx in test.txt file

Search for string in a file using grep command matching string even if it exists as part of word

Syntax

grep -w “string_to_search” file_name

Example

grep -w “infojinx” test.txt

The above command will search for word infojinx in test.txt file, not ignoring it even if it is part of a word.

Search and display count of matches found in file

Using this command one can search for lines which matches and display count of matching line found.

Syntax

grep -c “string_to_search” file_name

Example

grep -c “infojinx” test.txt

Search and display only matched string

By default grep searches and shows content of entire line where a match is found.  But you can use switch -o to see only matched string.

Syntax

grep -o “string_to_search” file_name

Example

grep -o “infojinx” test.txt

Search with inverted pattern

By using switch -v pattern of search will be inverted, i.e. lines which do not match with the searched pattern will be displayed. 

Syntax

grep -v “string_to_search” file_name

Example

grep -v “infojinx” test.txt

Search and display line with line number where match for string is found

Using switch -n search will be made for matching string. Display will done of line number followed by line.

Syntax

grep -n “string_to_search” file_name

Example

grep -n “infojinx” test.txt

Search for string at specific position in line

To search for a string at the start of a line use special character “^”.

Syntax

grep “^string_to_search” file_name

Example

grep  “^infojinx” test.txt

Above command will display all lines where infojinx is used in start of line.

To search for a string at the end of a line use special character “$”.

Syntax

grep “string_to_search$” file_name

Example

grep  “infojinx$” test.txt

Above command will display all lines where infojinx is used at end of line.

Search for special character in file

To search for any special character in a file, you need to escape them using “\” (backslash)

Syntax

grep “\special character” file_name

Example

grep  “\*” test.txt

This command will search for “*” in test.txt file.

Search for a range or set of characters in file

To search for a range of characters or set of characters use pair of [].

Syntax

grep “[string or range of characters]” file_name

Example

grep  “[amn]” test.txt

This will search for characters amn and line will be displayed if any of them are found.

grep”[a-d]” test.txt

This command will search any character from a to d and display line if any of them are found.