Grep Command in Linux: Syntax, Options & Grep Examples Commands!

Essential practical usage of Grep Command in Linux!

Grep Command in Linux


rep Command in Linux: Grep is powerful utility seaches files with given patterns. This Grep command utility comes with the latest Debian and non-Debian version by default. If your version misses this grep utility and then use the below command to install the grep searching utility in your Linux PC.

Install Grep on Ubuntu or Debian:

$sudo apt-get install grep

Install Grep on Fedora:

$sudo yum install grep

The above command will install the grep utility in your Linux PC. As we said earlier, Grep utility is mainly used to find the files based on the given pattern. Below we provided the essential practical usage of Grep Command in Linux.

Jump To:

Grep Command Basic Syntax:

The basic syntax of the Grep utility in Linux is,

grep [Options] [Pattern] [Sample-file]

The grep is the utility to search the files which match the pattern and outputs the results in the terminal.

Grep Command Options Table:

You can find the list of Grep options by executing the below command in the Linux Terminal.

$grep --help

The following output will appear on your Linux Terminal.

[email protected]:~$ 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-regexpPATTERNS are extended regular expressions
-F, --fixed-stringsPATTERNS are strings
-G, --basic-regexpPATTERNS are basic regular expressions
-P, --perl-regexpPATTERNS are Perl regular expressions
-e, --regexp=PATTERNSuse PATTERNS for matching
-f, --file=FILEtake PATTERNS from FILE
-i, --ignore-caseignore case distinctions
-w, --word-regexpmatch only whole words
-x, --line-regexpmatch only whole lines
-z, --null-dataa data line ends in 0 bytes, not a newline
Check this too  50+ Linux Command Sheet List for Newbies!


-s, --no-messagessuppress error messages
-v, --invert-matchselect non-matching lines
-V, --versiondisplay version information and exit
--helpdisplay this help text and exit

Output control:

-m, --max-count=NUMstop after NUM selected lines
-b, --byte-offsetprint the byte offset with output lines
-n, --line-numberprint line number with output lines
--line-bufferedflush output on every line
-H, --with-filenamethe print filename with output lines
-h, --no-filenamesuppress the file name prefix on output
--label=LABELuse LABEL as the standard input file name prefix
-o, --only-matchingshow only nonempty parts of lines that match
-q, --quiet, --silentsuppress all normal output
--binary-files=TYPEassume that binary files are TYPE; TYPE is ‘binary’, ‘text’, or ‘without-match’
-a, --textequivalent to --binary-files=text
-Iequivalent to --binary-files=without-match
-d, --directories=ACTIONhow to handle directories; ACTION is ‘read’, ‘recurse’, or ‘skip’
-D, --devices=ACTIONhow to handle devices, FIFOs, and sockets; ACTION is ‘read’ or ‘skip’
-r, --recursivelike --directories=recurse
-R, --dereference-recursivelikewise, but follow all symlinks
--include=GLOBSearch only files that match GLOB (a file pattern)
--exclude=GLOBskip files and directories matching GLOB
--exclude-from=FILEskip files matching any file pattern from FILE
--exclude-dir=GLOBskip directories that match GLOB
-L, --files-without-matchprint only names of FILEs with no selected lines
-l, --files-with-matchesprint only names of FILEs with selected lines
-c, --countprint only a count of selected lines per FILE
-T, --initial-tabmake tabs line up (if needed)
Z, --nullprint 0 byte after FILE name

Context control:

-B, --before-context=NUMprint NUM lines of leading context
-A, --after-context=NUMprint NUM lines of trailing context
-C, --context=NUMprint NUM lines of output context
-NUMsame as --context=NUM
--color[=WHEN], --color[=WHEN]use markers to highlight the matching strings; WHEN is ‘always’, ‘never’, or ‘auto’
-U, --binarydo not strip CR characters at EOL (MSDOS/Windows)
When FILE is '-', read standard input.  With no FILE, read '.' if
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.
Exit status is 0 if any line (or file if -L) is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to [email protected]
GNU grep home page: <>
General help using GNU software: <>

Search & Find Files using Grep Command:

Grep command allows us to search and find the files that installed or present in our Linux PC. Here we provided the sample grep command to perform the search operation to find the version of python installed on your Linux PC.

$dpkg -l | grep -i python
The above command outputs a result:

[email protected]:~$ dpkg -l | grep -i python
ii  deluge                                     1.3.15-2                             all          bittorrent client written in Python/PyGTK
ii  deluge-common                              1.3.15-2                             all          bittorrent client written in Python/PyGTK (common files)
ii  deluge-gtk                                 1.3.15-2                             all          bittorrent client written in Python/PyGTK (GTK+ ui)
ii  libboost-python1.67.0                      1.67.0-13                            amd64        Boost.Python Library

The above command dpkg -I which searches the installed “.Deb” packages on your Linux PC. Grep command will search and list the entries which contain “python” in it. The option -i is mainly used to ignore the case sensitive.

Check this too  Paste Command in Linux: Syntax, Commands & Examples!

Use Grep Command to perform Search & Filter the Files:

You can use the grep command to perform the search process and filter the files in your Linux PC. Grep command allows you to filter the individual or multiple files. Here in the below scenario, you have some trouble in “php.ini file”, by removing the “#” or “Uncomment” some codes will fix the trouble that you are facing. Then the below command will find the file and remove all the “#” in the file php.ini

$grep –v “#” /etc/apache2/php/php.ini

Grep Command to Find Mp4 files only:

You can simply use the grep command to find all the mp4 files in your Linux PC. Use the below command to do:

$find . –name “*.mp4” | grep –i Eminem | grep –vi “Parody”

The above command will find all the mp4 files which have the name “Eminem” and doesn’t print the file which contains “Parody” in it.

Prints the Number of Lines around the match:

The options -A & -B, which prints the matched line that come before or after the search string. -A stands for “After” & -B stands for “Before“. The options -C is similar but it displays the line in both directions.

$ifconfig | grep –A 4 tecrobust
$ifconfig | grep -B 2 eth1
$ifconfig | grep –C 2 eth0

Grep Command to count the number of matches:

Grep command allows you to count the number of matches in your Linux, Use the below command:

$ifconfig | grep –c trof1

Grep Command to search files by the given string:

You can perform search based on the given string. The option -n allows you to debugging files during compile errors. Use the below command:

$grep –n “main” index.php

Grep Command to search a string continuously in all directories:

Grep command allows you to perform the search recrusively in all directories. The -r option helps us to perform the recrusive search in all directories.

$grep –r “main” *

Grep Command to search entire pattern:

The -w option will allow you search the entire pattern in your Linux. Use the below command:

$ifconfig | grep –w “PATTERN”

Grep Command to search a string in zip files:

The Grep Command will allows you to search the given string inside the zip files. Use the following command:

$zgrep –i index /var/www/index/main.gz

Search with fixed string patterns & Regex in files:

The Grep command allows you to find the fixed string patterns & regex in files. Use the below command:

$grep –E

The option -E will allows you to perform the “Regular Expression” along with Grep Command.

$fgrep –f fixed_patterns.txt file_to_search.txt

The option -f allows you to perform the full fixed sting search option in Linux.

Check this too  20+ CURL Linux Commands 2019 For Ubuntu 18.04 & All Linux Distros


That’s it. In this article, we provided complete information about Grep Command, Grep Command Options & Essential Grep Command possible examples. If you have any queries related to this topic and then feel free to comment us below.

Article Review
  • Article Explanation
  • Commands Provided
  • Grep Options Table
  • Grep Examples


For more articles, Kindly visit our Homepage

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@]
Sub: Message to Julian

CHMOD Command in Linux

Chmod Command in Linux: File Permission Settings, Syntax & Examples!

GZip Command in Linux

GZip Command in Linux: Essential GZip Command Syntax & Examples