Skip to main content

Grep Cheat Sheet

Summary

Cheat sheet with the most common and useful Grep commands. Grep is a GNU/Linux command-line utility for searching plain-text data sets for lines that match a regular expression.

Syntax #

The general grep command syntax:

grep [OPTIONS] PATTERN [FILE...]

Searching #

Basic search in file #

grep 'pattern' file
grep -i 'pattern' file
grep -w 'word' file

Recursive search (for directories) #

grep -r 'pattern' directory

Search for multiple external matterns in a file #

grep -f patterns.txt file

Recursive search with line numbers and file names #

grep -rnH 'pattern' directory

Viewing #

Display Line Numbers #

grep -n 'pattern' file

Display Only Matching Part of Lines #

grep -o 'pattern' file

Display Filename with Matches #

grep -H 'pattern' file

Display only filenames with matches #

grep -l 'pattern' file

Display lines matching whole words only #

grep -wo 'word' file

Display lines before/after match #

grep -B 3 'pattern' file   # Display 3 lines before the match
grep -A 2 'pattern' file   # Display 2 lines after the match
grep -C 1 'pattern' file   # Display 1 line before and after the match

Display lines that match multiple patterns #

grep 'pattern1\|pattern2' file
grep -E 'pattern1|pattern2' file  # Using Extended Regular Expressions

Display context around matches #

grep -C 2 'pattern' file   # Display 2 lines of context around matches

Display lines matching multiple patterns (logical AND) #

grep 'pattern1' file | grep 'pattern2'

Display lines matching either of multiple patterns (logical OR) #

grep -E 'pattern1|pattern2' file

Display lines that do not match multiple patterns #

grep -v -e 'pattern1' -e 'pattern2' file

Invert match (exclude lines) #

grep -v 'pattern' file

Counting #

Count the number of matches #

grep -c 'pattern' file

Counting the occurrence of each matched line #

grep -c 'pattern' file | sort -n

These commands and options should cover a variety of scenarios when using grep to search for patterns in files. Customize them based on your specific needs and preferences.


Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

License: Grep Cheat Sheet by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/publications/grep-cheat-sheet">Grep Cheat Sheet</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the DITig legal page.


“Form ever follows function.”

Louis Henry Sullivan, American architectThe Tall Office Building Artistically Considered, - IT quotes