Master Linux grep: Essential Options and Real‑World Examples
This guide explains the Linux grep command, covering its basic syntax, a comprehensive list of useful options with descriptions, and practical examples that demonstrate how to search for strings, handle case sensitivity, display line numbers, count matches, and work with recursive and binary files.
The grep command is a powerful text‑search utility on Linux that supports regular expressions to locate matching lines in files.
Basic syntax
grep [options] [pattern] [files]Common options
-a, --text: Treat binary files as text. -c, --count: Show only the number of matching lines. -e pattern, --regexp=pattern: Specify a pattern; multiple patterns are allowed. -f file, --file=file: Read patterns from a file, one per line. -i, --ignore-case: Ignore case distinctions. -l, --files-with-matches: Print only the names of files with matches. -n, --line-number: Prefix each matching line with its line number. -r, --recursive: Recursively search sub‑directories. -v, --invert-match: Output lines that do **not** match. -x, --line-regexp: Match only whole lines. -w, --word-regexp: Match whole words only.
Practical examples
Search for a specific string:
grep "hello" file.txt
grep "hello" folder/file.txtSearch for multiple strings (OR logic): grep -E "hello|world" file.txt Ignore case: grep -i "hello" file.txt Show line numbers: grep -n "hello" file.txt Invert match (show non‑matching lines): grep -v "hello" file.txt Search a range of context lines (2 lines after match):
grep -A 2 "hello" file.txt # after
grep -B 2 "hello" file.txt # before
grep -C 2 "hello" file.txt # bothSearch whole words only: grep -w "hello" file.txt Count occurrences: grep -c "hello" file.txt Search specific file types:
grep "hello" *.txt
grep "hello" --include "*.txt" folder/Search recursively in sub‑directories:
grep -r "hello" folder/
grep -R "hello" folder/Force search in binary files: grep -a "hello" binary_file.bin Exclude specific directories: grep -r "hello" folder/ --exclude-dir=log/ Limit search to the first N lines:
grep -m 10 "hello" file.txt # only first 10 linesShow only the matching part of each line: grep -o "hello" file.txt Display non‑matching lines: grep -L "hello" file.txt Show surrounding context with -C: grep -C 2 "hello" file.txt Search multiple files at once: grep "hello" file1.txt file2.txt file3.txt Ignore surrounding whitespace when matching whole words: grep -w "hello" file.txt View upstream or downstream context:
grep -B 2 "hello" file.txt # before
grep -A 3 "hello" file.txt # afterSource: https://www.cnblogs.com/LanTianYou/p/17359397.html
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.
