Mastering Grep: 12 Powerful Linux Search Techniques
This guide walks through twelve practical grep and related command‑line techniques for Linux, covering how to list installed Python packages, filter configuration files, locate specific media files, display context lines, count matches, perform recursive and compressed‑file searches, and use extended or fixed‑string patterns with clear examples.
1. List installed Python packages
List all Python‑related RPM packages on the system.
# rpm -qa | grep -i pythonSample output includes entries such as python-kitchen-1.1.1-5.el7.noarch , python2-pip-8.1.2-14.el7.noarch , and many others.
2. Search and filter files
Remove all comment lines from an Nginx configuration file.
# grep -v "#" /etc/nginx/nginx.confThe -v option inverts the match, printing only lines that do not contain the # character.
3. Find only .mp3 files with multiple filters
Locate all .mp3 files, keep only those containing "jaychou" in the path, and exclude those containing "七里香".
# find . -name "*.mp3" | grep -i jaychou | grep -vi "七里香"4. Show lines before or after a match
Use -A to display a number of lines after a match and -B for lines before a match.
# ifconfig | grep -A 4 eth0
# ifconfig | grep -B 2 UP5. Print surrounding lines
The -C option prints a symmetric number of context lines around each match.
# ifconfig | grep -C 2 lo6. Count matches
Count how many times a pattern appears without piping to wc .
# ifconfig | grep -c inet67. Search files with line numbers
Show the line numbers of occurrences, useful for debugging compilation errors.
# grep -n "main" setup..py8. Recursive search in all directories
Search for a string throughout the current directory tree.
# grep -r "function" *9. Search whole‑word patterns
Match only whole words using -w . For example, searching for "RUNNING" returns matching lines, while searching for "RUN" does not because it is not a whole word.
# ifconfig | grep -w "RUNNING"
# ifconfig | grep -w "RUN"10. Search inside compressed files
zgrep works like grep on .gz files, using the same options.
# zgrep -i error /var/log/rumenz.gz11. Use extended regular expressions
egrep (or grep -E ) enables additional metacharacters such as + , ? , | , and parentheses.
# grep -E "pattern" file12. Search fixed‑string patterns
fgrep (equivalent to grep -F ) searches for literal strings without interpreting regular expressions, offering faster performance for exact matches.
# fgrep strcpy *.cSigned-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.
