40 Essential grep Commands Every Linux User Should Master
This guide presents a comprehensive collection of 40 practical grep examples—from basic searches and case‑insensitive matching to complex regular‑expression patterns and advanced options—showing how to quickly locate, filter, and manipulate text in files and logs on Linux systems.
Grep is a powerful text‑search utility in Linux that acts like a magnifying glass for quickly finding lines containing specific patterns in large files. It is indispensable for developers, system administrators, and anyone who needs to analyze logs or extract data from text.
Basic Usage
grep "hello" file.txt– Search for lines containing the word hello in file.txt. grep -i "hello" file.txt – Perform a case‑insensitive search, matching Hello , HELLO , etc. grep -w "hello" file.txt – Match the whole word hello only, excluding hello-world or helloworld . grep -v "error" file.txt – Show all lines that do not contain the word error . grep -n "hello" file.txt – Display matching lines with their line numbers. grep -c "hello" file.txt – Count how many lines contain hello without printing the lines. grep -r "hello" /path/to/dir/ – Recursively search for hello in a directory tree. grep -l "hello" *.txt – List only the names of files that contain hello . grep -h "hello" *.txt – Suppress file names when searching multiple files. grep -o "hello" file.txt – Output only the matching portion of each line.
Regular‑Expression Examples
grep "^hello" file.txt– ^ anchors the pattern to the start of a line. grep "world$" file.txt – $ anchors the pattern to the end of a line. grep "^$" file.txt – Match empty lines. grep "." file.txt – . matches any single character (non‑empty lines). grep "he..o" file.txt – Two dots represent any two characters between he and o . grep "h[aeiou]llo" file.txt – [ ] matches any one of the listed characters, covering hallo , hello , hillo , hollo , hullo . grep "[0-9]" file.txt – Find lines containing any digit. grep "[a-z]" file.txt – Find lines containing any lowercase letter. grep "[A-Z]" file.txt – Find lines containing any uppercase letter. grep "hello\|world" file.txt – \| works as logical OR, matching either hello or world . grep "he*" file.txt – * means the preceding character appears zero or more times (matches h , he , hee , …). grep "he\+" file.txt – \+ requires at least one occurrence of the preceding character. grep "he\?" file.txt – \? makes the preceding character optional (matches h or he ). grep -E "hello{2,}" file.txt – With -E (extended regex), {2,} means the preceding character repeats at least twice (matches helloo , hellooo , …).
Advanced Techniques
grep -A 2 "error" file.txt– Show the matching line plus the two lines after it. grep -B 2 "error" file.txt – Show the matching line plus the two lines before it. grep -C 2 "error" file.txt – Show two lines of context both before and after the match. ps aux | grep "nginx" – Pipe the output of ps aux to grep to filter processes related to nginx . history | grep "ssh" – Search your command history for entries containing ssh . grep -e "error" -e "fail" file.txt – Use multiple -e patterns to search for either word. grep -f patterns.txt file.txt – Read patterns from patterns.txt, one per line. grep --color=auto "hello" file.txt – Highlight matches in colour for easier visual scanning. grep -q "success" file.txt && echo "Found!" – Quiet mode for scripting; execute a command only if a match is found. grep -s "hello" maybe_no_exist.txt – Suppress error messages for missing or unreadable files. grep -H "hello" *.txt – Force printing of file names even when searching a single file. ls -l | grep "^d" – List only directory entries (lines starting with d). grep -P "\x68\x65\x6c\x6c\x6f" file.txt – Use Perl‑compatible regex to search for the hex representation of hello . grep -m 5 "info" huge_log.txt – Stop after the first five matches to avoid overwhelming output. grep -Z -r "secret" . | xargs -0 rm – Combine -Z (null‑terminated output) with xargs -0 to safely delete all files containing the word secret (use with extreme caution).
By experimenting with these 40 examples in a terminal, you can quickly become proficient with grep, turning it into a versatile tool for searching, filtering, and processing text across a wide range of Linux tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
