1/9/24, 7:13 AM Mastering grep Command: Your Complete Cheat Sheet for Efficient Text Searching in Linux | by Cuncis
ux | by Cuncis | Medium
Open in app Sign up Sign in
Search
Mastering grep Command: Your Complete
Cheat Sheet for Efficient Text Searching in
Linux
Cuncis · Follow
3 min read · Feb 26, 2023
Listen Share
grep is a powerful command-line utility in Linux and Unix operating systems that
allows you to search for specific patterns of text within one or more files. The name
"grep" stands for "global regular expression print".
Here’s the basic syntax of the grep command:
grep [options] pattern [file ...]
Where:
[options] : optional arguments that modify the behavior of the grep command
https://medium.com/@cuncis/mastering-grep-command-your-complete-cheat-sheet-for-efficient-text-searching-in-linux-b569a573432b 1/11
1/9/24, 7:13 AM Mastering grep Command: Your Complete Cheat Sheet for Efficient Text Searching in Linux | by Cuncis | Medium
pattern : the text pattern you want to search for
[file ...] : the file or files you want to search for the pattern
The grep command works by reading the contents of the specified file or files and
searching for lines that match the specified pattern. It then prints out the lines that
match the pattern to the standard output (usually the terminal window).
Here’s a cheat sheet for the grep command:
Basic Usage:
grep pattern file : Search for pattern in file
grep pattern file1 file2 file3 : Search for pattern in multiple files
grep pattern *.txt : Search for pattern in all files with the .txt extension
Options:
-i : Ignore case (case-insensitive search)
-w : Match whole word
-n : Show line numbers
-c : Count the number of matching lines
-v : Invert match, show lines that do not match
-r : Search files recursively in subdirectories
-l : Show only the filenames of matching files
-h : Do not show filenames in output
-e pattern : Use pattern as the search pattern
-f file : Read the search pattern from a file
-E : Interpret the pattern as an extended regular expression
-P : Interpret the pattern as a Perl-compatible regular expression
-m num : Stop after finding num matches
https://medium.com/@cuncis/mastering-grep-command-your-complete-cheat-sheet-for-efficient-text-searching-in-linux-b569a573432b 2/11