0% found this document useful (0 votes)
105 views2 pages

Grep Cheat Sheet

The document provides a cheat sheet for using the grep command in Linux to search for text patterns within files. It explains that grep allows searching for specific text patterns in one or more files. The basic syntax and components of the grep command are described, including optional arguments that modify its behavior. A number of useful grep options are also listed, such as ignoring case, matching whole words, showing line numbers, inverting matches, and more.

Uploaded by

bogeybogey6891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views2 pages

Grep Cheat Sheet

The document provides a cheat sheet for using the grep command in Linux to search for text patterns within files. It explains that grep allows searching for specific text patterns in one or more files. The basic syntax and components of the grep command are described, including optional arguments that modify its behavior. A number of useful grep options are also listed, such as ignoring case, matching whole words, showing line numbers, inverting matches, and more.

Uploaded by

bogeybogey6891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like