grep Command in Linux

grep Command in Linux

Grep command in Linux, with its most frequently used command options.

In this article, we explain one of the basic Linux commands for beginners, i.e. the grep command in Linux, with its most frequently used command options.

What is Linux?

Linux is a family of open-source operating systems based on the Linux kernel. The first Linux system kernel was released on September 17, 1991, by Linus Torvalds.

Read more …

Popular Linux distributions include Debian, Fedora, and Ubuntu, and the commercial distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server.

There are also quite a number of customized Linux distributions, such as Kali Linux, REMnux etc. Kali Linux is a Debian-based distribution developed, funded and maintained by Offensive Security for ethical hackers for the purposes of Penetration Testing, Security Research & Assessment, and Computer Computer Forensics & Reverse Engineering. REMnux, on the other hand, is a Linux distro curated for reverse-engineering and malware analysis purposes.

Quote by Linus Torvalds

What I find most interesting is how people really have taken Linux and used it in ways and attributes and motivations that I never felt.

Linus Torvalds

Read more educational and inspirational cyber quotes at our page 100+ Best Cyber Security & Hacker Quotes.

grep Command in Linux – Print Lines That Match Patterns

In Linux, grep is a command-line utility to search for a string of characters, i.e., patterns, in a specified file or files. The search strings could vary from simple words to complex regular expressions. When a match is found, the line containing the search string is printed as the grep command’s output. The grep command is specifically useful for searching a string of characters in large files, such as log files.

Search for PATTERNS in each FILE. Basic usage: grep [OPTION] ... PATTERNS [FILE] ... Use -c to suppress normal output and print a count of matching lines instead, use -i to ignore case distinctions in patterns, -n to prefix each line of output with a line number, and -v (invert match) to select non-matching lines.

Using the grep Command

To demonstrate the most frequently used grep command options, we will be using the files illustrated in Figure 1 and 2 in the following examples.

Figure 1: Contents of the Demonstration File 1
Figure 1: Contents of the Demonstration File 1
Figure 2: Contents of the Demonstration File 2
Figure 2: Contents of the Demonstration File 2

Simple String Search

To print any line from a file that contains the specified pattern (case sensitive), write grep followed by the search pattern and the file name, e.g., grep pattern file-name.

$ grep string file1.txt
Figure 3: Simple String Search with grep Command
Figure 3: Simple String Search with grep Command

Searching a Pattern in Multiple Files

To print any line from multiple files that contain the specified pattern (search string), write the file names with spaces in between.

$ grep string file1.txt file2.txt
Figure 4: Searching a Pattern in Multiple Files
Figure 4: Searching a Pattern in Multiple Files

Searching a Pattern Recursively in a Directory

To search a pattern recursively in multiple files in a directory, use the -r option followed by the search pattern and the directory name.

$ grep -r string grep/
Figure 5: Searching a Pattern Recursively in All Files in a Directory
Figure 5: Searching a Pattern Recursively in All Files in a Directory

Searching a Pattern with Ignore Case Option

To print any line from a file that contains the specified pattern, disregarding the letter case (case insensitive), use the -i option.

$ grep -i string file1.txt
Figure 6: Searching a Pattern with Ignore Case Option
Figure 6: Searching a Pattern with Ignore Case Option

Inverse Search

To print lines that does not match the specified pattern (invert match), use the -v option.

$ grep -v string file1.txt
Figure 7: Searching a Pattern with Invert Match Option
Figure 7: Searching a Pattern with Invert Match Option

Note that, the preceding example can be further filtered with a case insensitive invert match search, by using the -i and -v options together, as demonstrated in Figure 8.

$ grep -vi string file1.txt
Figure 8: Searching a Pattern with Ignore Case and Invert Match Options
Figure 8: Searching a Pattern with Ignore Case and Invert Match Options

Displaying Line Numbers

To prefix each of the matching lines with a line number, use the -n option.

$ grep -n string file1.txt
Figure 9: Printing Line Numbers for the Matching Lines
Figure 9: Printing Line Numbers for the Matching Lines

Printing Count of Matching Lines

To suppress the normal output and print a count of matching lines instead, use the -c option.

$ grep -c string file1.txt
Figure 10: Printing a Count of Matching Lines
Figure 10: Printing a Count of Matching Lines

Printing Lines with Whole Word Matches

To select only those lines containing matches that form whole words, use the -w option.

$ grep -w hack file1.txt file2.txt
Figure 11: Printing Lines with Whole Word Matches
Figure 11: Printing Lines with Whole Word Matches

Listing Names of Files

To suppress the normal output and instead print the names of each input file that would contain the search pattern, use the -l option. To print the names of files that would not contain the pattern, use the -L option.

$ grep -l hack file1.txt file2.txt
Figure 12: Listing Names of Files that Contain the Search Pattern in the File Contents
Figure 12: Listing Names of Files that Contain the Search Pattern in the File Contents

Displaying a Number of Files Before and After a Matching Line

To display a specified number of lines after a matching line, use the -A option followed by a number. Similary, use the -B option to display a specified number of lines before a matching line, and use the -C option to display a specified number of lines both before and after a matching line.

$ grep -A 3 hack file1.txt file2.txt
Figure 13: Displaying a Specified Number of Files Before and After a Matching Line
Figure 13: Displaying a Specified Number of Files Before and After a Matching Line

Piping the Output of a Command into the grep

Note that, in Linux everything is represented as a file. In other words, files, directories, device or outputs generated by commands are all files, albeit with different functionalities. In this context, output generated by a command can also be fed into to the grep command by using the pipe (|) operator. In fact, this is one of the most common uses of the grep command.

For instance, to search for the root user in Kali Linux in the /etc/passwd file, you can use the following command:

$ cat /etc/passwd | grep -w root

is equivalent to

$ grep -w root /etc/passwd
Figure 14: Piping the Output of a Command into the grep Command
Figure 14: Piping the Output of a Command into the grep Command

To explore more grep command options, please visit the man pages by typing $ man grep on the terminal.

To learn more on Linux, you could also visit our Linux Resources Page.