Glossary

grep

grep (from ed's g/re/p command: "globally search a regular expression and print") is the tool for searching text. It reads files or stdin line by line and prints lines matching a given pattern. Ken Thompson wrote the original in 1973, and it has been a cornerstone of Unix ever since.

grep pattern file.txt
grep -i pattern file.txt          # case insensitive
grep -r pattern dir/              # recurse into directories
grep -n pattern file.txt          # show line numbers
grep -v pattern file.txt          # invert match
grep -E 'foo|bar' file.txt        # extended regex (alias egrep)
grep -F 'literal.string' file     # fixed string, no regex (fgrep)
grep -l pattern *.txt             # list files with matches
grep -c pattern file.txt          # count matching lines
grep --color=auto pattern file    # highlight matches

Modern replacements like ripgrep (rg) and The Silver Searcher (ag) are faster and smarter by default (skipping ignored files, respecting .gitignore), but they read grep's regex syntax and are broadly compatible. For scripting and minimum portability, plain grep is still the right choice.

Combined with pipes, grep is the core of countless Linux idioms: ps aux | grep nginx, journalctl | grep -i error, env | grep PATH. It is worth learning its flags well.

Related terms: sed, awk, Regular Expression

Discussed in:

Also defined in: Textbook of Linux