Note that, in Linux everything is represented as a file. $ grep -A 3 hack file1.txt file2.txt Figure 13: Displaying a Specified Number of Files Before and After a Matching Line Piping the Output of a Command into the grep 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. To display a specified number of lines after a matching line, use the -A option followed by a number. $ grep -l hack file1.txt file2.txt 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 print the names of files that would not contain the pattern, use the -L option. To suppress the normal output and instead print the names of each input file that would contain the search pattern, use the -l option. $ grep -w hack file1.txt file2.txt Figure 11: Printing Lines with Whole Word Matches Listing Names of Files To select only those lines containing matches that form whole words, use the -w option. $ grep -c string file1.txt Figure 10: Printing a Count of Matching Lines Printing Lines with Whole Word Matches To suppress the normal output and print a count of matching lines instead, use the -c option. $ grep -n string file1.txt Figure 9: Printing Line Numbers for the Matching Lines Printing Count of Matching Lines To prefix each of the matching lines with a line number, use the -n option. $ grep -vi string file1.txt Figure 8: Searching a Pattern with Ignore Case and Invert Match Options Displaying Line Numbers 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 -v string file1.txt Figure 7: Searching a Pattern with Invert Match Option To print lines that does not match the specified pattern (invert match), use the -v option. $ grep -i string file1.txt Figure 6: Searching a Pattern with Ignore Case Option Inverse Search
![grep options linux grep options linux](https://www.junosnotes.com/wp-content/uploads/2021/07/grep-version.png)
To print any line from a file that contains the specified pattern, disregarding the letter case (case insensitive), use the -i option. $ grep -r string grep/ Figure 5: Searching a Pattern Recursively in All Files in a Directory Searching a Pattern with Ignore Case Option 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 string file1.txt file2.txt Figure 4: Searching a Pattern in Multiple Files Searching a Pattern Recursively in a Directory 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 Figure 3: Simple String Search with grep Command Searching a Pattern in Multiple Files
![grep options linux grep options linux](https://queirozf.com/images/contents/crLW7et.png)
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. Figure 1: Contents of the Demonstration File 1 Figure 2: Contents of the Demonstration File 2 Simple String Search 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. 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. The grep command is specifically useful for searching a string of characters in large files, such as log files.
![grep options linux grep options linux](https://www.linuxcommands.site/wp-content/uploads/2020/04/image-3-1024x364.png)
When a match is found, the line containing the search string is printed as the grep command’s output. The search strings could vary from simple words to complex regular expressions. In Linux, grep is a command-line utility to search for a string of characters, i.e., patterns, in a specified file or files. grep Command in Linux – Print Lines That Match Patterns Read more educational and inspirational cyber quotes at our page 100+ Best Cyber Security & Hacker Quotes.