Skip to content

Very important Grep commands in Linux

15 essential grep commands

grep (global regular expression print) is a very useful command to search a matching pattern in a file in Linux Operating System. This command comes pre-installed with Linux Operating system. With the help of grep command, we can filter our required strings from log files and get the desired output. On this writing I am going to describe usages of 14 most essentials command.

1. Grep Command syntax

The syntax of the grep command is as follows:

grep [option…] PATTERNS [FILE…]

2. Search a string in a file.

$ grep “root” /etc/passwd

Output:

14 Essentials Grep command in Linux

3. Search a string in multiple files

grep <search string> <file name>

For Example:

The below command will search a string named “error” in file “kern.log” and kern.log.1″

$ grep -i error /var/log/kern.log kern.log.1

Output:

14 Essentials Grep command in Linux

4. Search Recursively in all directories

To search a string in current directory and all other sub-directories, using -r flag as below

$ grep -r “string” *

For Example: The below command will search the string “errors” recursively inside /var/log directory.

$ grep -r "error" /var/log/

Output:

5. Search multiple strings

To search multiple keyword / strings we can use -e flag

grep -e “strings” -e “strings” <field name>

For Example:

To find multiple strings “error” and “ras” in the log file “var/log/kern.log“, we use below command.

$ grep -i -e “error” -i -e “ras” /var/log/kern.log

Output:

6. Ignore Case

To ignore case sensitivity of seacched string and find all matched strings, we can use -i flag.

grep -i <serach string> <file name>

For Example:

Below command will list all the occurrences of string “error” in the log file

$ grep -i error /var/log/kern.log

Output:

7. Number the Lines that matches search string

To get the line number of search matched results, we can use -n flag.

grep -n “serach string” <file name>

For Example:

below command will display the line no, where search string matched

$ grep -n -i error /var/log/kern.log

Output:

8. Display lines before and after the match string

We can use -A and -B flag to print required lines from the matched string

grep -i -A 3 -B 3 “strings” <file name>

For Example:

Below command will print 3 lines above and below the matched string “ERROR” in a “/var/log/dmesg log file

$ grep -A 3 -B 3 “error” /var/log/dmesg

Output:

9. Invert Match

To print the lines that are not match, we can use -v flag.

grep -v “string” file-name

For Example:

print all lines that do not contain the string “root”

$ grep -v “root” /etc/passwd

Output:

10. Find Exact Match

To search the exact match of the string we can use -w flag

grep -w “string” file

For Example:

Below command search all the files inside /etc directory and find the exact word “rootfs

$grep -r -w “rootfs” /etc/

Output:

11. Find Number of occurrences

To find the number of occurances of the search string we can use -c flag

grep -rc -w “string” filename

For Example:

To get the number of occurances of search string “rootfs” in a file we can use the below command

$ grep -rc -w “rootfs” /etc/apparmor.d/usr.lib.snapd.snap-confine.real

Output:

12. find name of files with matching string

To get the file names where search “string” is found inside a directory, we can us l flag

grep -l “string <file name>

For Example:

Below command will display the name of files where search string “error” is found inside directory “/var/log”

$ grep -l -i error /var/log/*

Output:

13. suppress error messages.

We suppress error messages of grep command output with -s flag

grep -s “string” <fle name>

$ grep -s “root.*bash” /etc/passwd1

Output:

14. Grep with Regular Expression

^ Matches characters at the beginning of a line
$ Matches characters at the end of a line
“.” Matches any character
[a-z] Matches any characters between A and Z
[^ ..] Matches anything apart from what is contained in the bracke

$ grep -i “^r” /etc/passwd

$grep “root.*bash” /etc/passwd

To get all the non-commented line of a file “/etc/fstab

$ grep -v “^#” /etc/fstab

Conclusion

Grep command helps in searching the log files very quick with different available flags, which makes the life of a system admin very easy. To know more depth of grep command, you can use the man page or use grep –help

Tags: