How to use grep command in Linux / Debian with simple and easy examples.
grep stands for “global regular expression print” which is used to search files for lines that contain a pattern. By default, this command is used to display the matching lines in the terminal. It is considered as one of the most useful commands on Linux and Unix.
It comes equipped on every distribution of Linux. If it is not installed on your operating system, then you can install it easily using the following commands:
For CentOS/Red Hat (RHEL)/Fedora
yum install grep
sudo apt-get install grep
grep [OPTIONS] PATTERN [FILE…]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE…]
The easiest way to learn how to use the “global regular expression print” command is by showing examples, so let’s do it.
1.) How to use grep command to search a file
grep "nobody" /etc/passwd nobody:x:99:99:Nobody:/:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
2.) Case insensitive search
grep -i "nobody" /etc/passwd
-i option ignores case distinctions
3.) Search string in multiple files
cat filename1 Hello Test cat filename2 Hello Test grep "Test" filename* Output will be: filename1:Hello Test filename2:Hello Test
4.) Count the number of matching patterns
grep -c Hello filename1 1
The above command with option -c counts how many lines match the given pattern.
5.) Recursive Search
grep -r "cron" /etc
For example, in the above command –r option is used which will search string “cron” recursively in /etc directory along with all of the subdirectories under /etc directory.
6.) Line Numbers of Successful Matches
Let’s create a text file named “filename3” with some text. For example, the first line will be “First Reading this file”, Then the Second line will be “Second Writing this file1” and the third line will be “Third Overwriting this file2”.
cat filename3 First Reading this file Second Writing this file1 Third Overwriting this file2 grep -n "file1" filename3 2:Second Writing this file1
7.) Searching the lines that start with a string
cat numbers.txt 122 222 322 455 5 430 7 grep "^3" numbers.txt 322
8.) Searching the lines that end with a string
cat numbers.txt 122 222 322 455 5 430 7 grep "5$" numbers.txt 455 5
9.) Search multiple patterns using
cat numbers.txt 122 222 322 455 5 430 7 grep -e "122" -e 455 numbers.txt 122 455
10.) Search a fixed pattern with -f option
cat filename1 Hello Test cat filename2 Hello Test grep -f filename1 filename2 Hello Test
11.) Return everything excluding the pattern with -v option
cat filename3 First Reading this file Second Writing this file1 Third Overwriting this file2 grep -v "file2" filename3 First Reading this file Second Writing this file1
12.) Search blank lines of a file
grep "^$" emptyfile.txt
If selected lines are found, then the exit status is 0.
Otherwise, the exit status is 1.
If an error occurs, then the exit status is 2.
Few Useful Options List From Man Page
It suppresses the conventional output; instead, show matching lines count for every input file. With the -v, –invert-match option, it counts non-matching lines. (-c is specified by POSIX.)
It surrounds the matched non-empty strings, context lines, matching lines, file names, byte offsets, line numbers, and separators for groups and fields of context lines with escape sequences to print them in colors on the terminal. The colors are defined by a variable named, “GREP_COLORS”. The deprecated environment variable named, “GREP_COLOR” is still supported, but it’s settings now does not have a priority. WHEN is never, always, or auto.
-L option suppresses normal output; instead, it prints the name of every input file from which normally no output would have printed normally. The scanning will stop once the first match is found.
-l suppresses traditional output on the terminal; instead, it prints the name of every input file from that output would have been printed normally. With this option, the scanning will stop on the first match. (-l is specified by POSIX.)
-o display only the matched non-empty parts of a line which is matched on the terminal.
-q, –quiet, –silent
-q –quiet; It does not write anything to stdout on the terminal. If a match is found or even an error was detected. It will exit with zero statuses immediately. Also, check the -s or –no-messages option. (-q is specified by POSIX.)
If you find this post helpful, please share it on your social networks.
Have any questions? Leave a comment below.
Thanks for reading!