in

LinuxLinux

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

Essential practical usage of Grep Command in Linux!

Grep Command in Linux
Reading Time: 6 minutes

G

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-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
-w, --word-regexp match only whole words
-x, --line-regexp match only whole lines
-z, --null-data a data line ends in 0 bytes, not a newline
Check this too  10 Interesting Usages of "!" Operator in Linux Commands

Miscellaneous:

-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit

Output control:

-m, --max-count=NUM stop after NUM selected lines
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename the print filename with output lines
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only nonempty parts of lines that match
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE; TYPE is ‘binary’, ‘text’, or ‘without-match’
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories; ACTION is ‘read’, ‘recurse’, or ‘skip’
-D, --devices=ACTION how to handle devices, FIFOs, and sockets; ACTION is ‘read’ or ‘skip’
-r, --recursive like --directories=recurse
-R, --dereference-recursive likewise, but follow all symlinks
--include=GLOB Search only files that match GLOB (a file pattern)
--exclude=GLOB skip files and directories matching GLOB
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=GLOB skip directories that match GLOB
-L, --files-without-match print only names of FILEs with no selected lines
-l, --files-with-matches print only names of FILEs with selected lines
-c, --count print only a count of selected lines per FILE
-T, --initial-tab make tabs line up (if needed)
Z, --null print 0 byte after FILE name

Context control:

-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--color[=WHEN], --color[=WHEN] use markers to highlight the matching strings; WHEN is ‘always’, ‘never’, or ‘auto’
-U, --binary do 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: <http://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>

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  50+ Linux Command Sheet List for Newbies!

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  URL Encode & Decode on Linux using Command Terminal!

Verdict:

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
4.9

Summary

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 [@] gmail.com
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