Master grep: Essential Linux Text Search Commands and Tips
This guide explains how to use the Linux grep command for powerful text searching, covering common options, file content searches, recursive scans, exclusion patterns, case‑insensitive matching, context lines, rule‑file searches, and regular‑expression techniques with practical examples.
Introduction
The article introduces grep , a fundamental Linux command for searching text within files and command output, complementing earlier tutorials on other Linux utilities.
Common Usage
Typical usage combines grep with a pipe ( |) to filter the output of previous commands. Example: $ ps -ef | grep redis This filters the process list to show only lines containing redis. The -v option excludes matching lines, and -c returns a count:
$ ps -ef | grep redis | grep -v auto $ ps -ef | grep redis -cSearching File Contents
To search within a specific file, use: $ grep "test" aaa/bbb/linux_command_debug.md Adding -n shows line numbers:
$ grep -n "test" aaa/bbb/linux_command_debug.mdSearching Multiple Files or Excluding Files
Search all .md files: $ grep -n "test" *.md Exclude files with --exclude or directories with --exclude-dir:
$ grep -rn "test" --exclude=*.txt $ grep -rn "test" --exclude-dir=aaaMultiple exclusion patterns can be stored in a file and referenced with --exclude-from:
$ grep -rn "test" --exclude-from=skip.txtFinding Files Containing a Keyword
Recursively list files that contain a pattern using -l (or --file-with-matches):
$ grep -rln "int main(void)"Finding Files Not Containing a Keyword
Use -L to list files that do not match the pattern:
$ grep -rLn "int main(void)"Case‑Insensitive Search
Apply -i (or --ignore-case) to ignore case:
$ grep -rni "int MAIN(void)"Showing Context Lines
Show lines before and after a match with -B and -A (or --before-context / --after-context):
$ grep -rn "int main(void)" -A 1 -B 1Using a Rule File
Store multiple patterns in a file (e.g., key.txt) and search with -f:
$ cat filename | grep -f key.txtRegular‑Expression Search
grepsupports regular expressions. For example, to find lines that start and end with t: $ grep -rn ^t.*t$ Extended regular expressions can be used via egrep.
Summary of Frequently Used Options
-v Show lines that do NOT match the pattern.
-l List files containing matches.
-L List files without matches.
-r Recursive search.
-i Ignore case.
-n Show line numbers.
-A n Show n lines after a match.
-B n Show n lines before a match.
--exclude Exclude specific files.
--exclude-dir Exclude specific directories.
-f Read patterns from a file.
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.
