Mastering grep: Essential Options and Real‑World Examples
This guide explains the Linux grep command, detailing its purpose, syntax, and a comprehensive list of options such as -i, -r, -c, and -w, followed by practical examples that demonstrate searching for strings, handling case sensitivity, counting matches, recursive directory scans, and displaying context lines.
Introduction
grep is a widely used Linux text-search tool that searches for patterns in files and prints matching lines. It stands for “global regular expression print” and supports regular expressions.
Options
All grep options are listed below:
grep [options] [pattern] [files] -a, --text: treat binary files as text. -c, --count: show only the count of matching lines. -e pattern, --regexp=pattern: specify a pattern; multiple patterns 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 line with its line number. -r, --recursive: recursively search subdirectories. -v, --invert-match: select non‑matching lines. -x, --line-regexp: match only whole lines. -w, --word-regexp: match only whole words. pattern is usually a regular expression, and files can be one or more files or directories.
Common grep option examples
Search for a specific string:
grep "hello" file.txt
grep "hello" folder/file.txtSearch multiple strings with -E: grep -E "hello|world" file.txt Ignore case with -i: grep -i "hello" file.txt Show line numbers with -n: grep -n "hello" file.txt Invert match with -v: grep -v "hello" file.txt Show surrounding lines with -A, -B, -C:
grep -A 2 "hello" file.txt
grep -B 2 "hello" file.txt
grep -C 2 "hello" file.txtMatch whole word with -w: grep -w "hello" file.txt Count matches with -c: grep -c "hello" file.txt Search specific file types:
grep "hello" *.txt
grep "hello" --include="*.txt" folder/Search subdirectories with -r:
grep -r "hello" folder/
grep -R "hello" folder/Search binary files with -a: grep -a "hello" binary_file.bin Exclude directories with --exclude-dir: grep -r "hello" folder/ --exclude-dir=log/ Limit number of lines searched with -m: grep -m 10 'hello' file.txt Show only matched strings with -o: grep -o 'hello' file.txt Show non‑matching lines with -L: grep -L 'hello' file.txt Search multiple files at once: grep 'hello' file1.txt file2.txt file3.txt Ignore surrounding whitespace with -w (matches whole words, effectively ignoring surrounding spaces).
View context with -B and -A:
grep -B 2 'hello' file.txt
grep -A 3 'hello' file.txtSigned-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
