Grep 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:
Install Grep on Fedora:
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.
- Grep Command Syntax
- Grep Command Options Table
- Search & Find Files using Grep Command
- Use Grep Command to perform Search & Filter the Files
- Grep Command to Find Mp4 files only
- Prints the Number of Lines around the match
- Grep Command to count the number of matches
- Grep Command to search files by the given string
- Grep Command to search a string continuously in all directories
- Grep Command to search entire pattern
- Grep Command to search a string in zip files
- Search with fixed string patterns & Regex in files.
Grep Command Basic Syntax:
The basic syntax of the Grep utility in Linux is,
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.
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, ||PATTERNS are extended regular expressions|
|-F, ||PATTERNS are strings|
|-G, ||PATTERNS are basic regular expressions|
|-P, ||PATTERNS are Perl regular expressions|
|-e, ||use PATTERNS for matching|
|-f, ||take PATTERNS from FILE|
|-i, ||ignore case distinctions|
|-w, ||match only whole words|
|-x, ||match only whole lines|
|-z, ||a data line ends in 0 bytes, not a newline|
|-s, ||suppress error messages|
|-v, ||select non-matching lines|
|-V, ||display version information and exit|
|display this help text and exit|
|-m, ||stop after NUM selected lines|
|-b, ||print the byte offset with output lines|
|-n, ||print line number with output lines|
|flush output on every line|
|-H, ||the print filename with output lines|
|-h, ||suppress the file name prefix on output|
|use LABEL as the standard input file name prefix|
|-o, ||show only nonempty parts of lines that match|
|-q, ||suppress all normal output|
|assume that binary files are TYPE; TYPE is ‘binary’, ‘text’, or ‘without-match’|
|-a, ||equivalent to |
|-I||equivalent to |
|-d, ||how to handle directories; ACTION is ‘read’, ‘recurse’, or ‘skip’|
|-D, ||how to handle devices, FIFOs, and sockets; ACTION is ‘read’ or ‘skip’|
|-r, ||like |
|-R, ||likewise, but follow all symlinks|
|Search only files that match GLOB (a file pattern)|
|skip files and directories matching GLOB|
|skip files matching any file pattern from FILE|
|skip directories that match GLOB|
|-L, ||print only names of FILEs with no selected lines|
|-l, ||print only names of FILEs with selected lines|
|-c, ||print only a count of selected lines per FILE|
|-T, ||make tabs line up (if needed)|
|Z, ||print 0 byte after FILE name|
|-B, ||print NUM lines of leading context|
|-A, ||print NUM lines of trailing context|
|-C, ||print NUM lines of output context|
|-NUM||same as |
|use markers to highlight the matching strings; WHEN is ‘always’, ‘never’, or ‘auto’|
|-U, ||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.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.
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 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:
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.
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:
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 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 Command to search entire pattern:
The -w option will allow you search the entire pattern in your Linux. Use the below command:
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:
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:
The option -E will allows you to perform the “Regular Expression” along with Grep Command.
The option -f allows you to perform the full fixed sting search option in Linux.
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.
For more articles, Kindly visit our Homepage