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:
||PATTERNS are extended regular expressions|
||PATTERNS are strings|
||PATTERNS are basic regular expressions|
||PATTERNS are Perl regular expressions|
||use PATTERNS for matching|
||take PATTERNS from FILE|
||ignore case distinctions|
||match only whole words|
||match only whole lines|
||a data line ends in 0 bytes, not a newline|
||suppress error messages|
||select non-matching lines|
||display version information and exit|
||display this help text and exit|
||stop after NUM selected lines|
||print the byte offset with output lines|
||print line number with output lines|
||flush output on every line|
||the print filename with output lines|
||suppress the file name prefix on output|
||use LABEL as the standard input file name prefix|
||show only nonempty parts of lines that match|
||suppress all normal output|
||assume that binary files are TYPE; TYPE is ‘binary’, ‘text’, or ‘without-match’|
||how to handle directories; ACTION is ‘read’, ‘recurse’, or ‘skip’|
||how to handle devices, FIFOs, and sockets; ACTION is ‘read’ or ‘skip’|
||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|
||print only names of FILEs with no selected lines|
||print only names of FILEs with selected lines|
||print only a count of selected lines per FILE|
||make tabs line up (if needed)|
||print 0 byte after FILE name|
||print NUM lines of leading context|
||print NUM lines of trailing context|
||print NUM lines of output context|
||use markers to highlight the matching strings; WHEN is ‘always’, ‘never’, or ‘auto’|
||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