Mastering grep: 12 Powerful Ways to Search and Filter Files on Linux
This guide introduces grep—a ubiquitous Linux command‑line tool—and demonstrates twelve practical techniques for searching, filtering, counting, and automating text processing tasks across files, directories, and compressed logs.
Grep is a powerful file pattern search tool preinstalled on every Linux distribution. If it is missing, you can install it via the package manager (apt‑get for Debian/Ubuntu, yum for RHEL/CentOS/Fedora).
sudo apt-get install grep # Debian/Ubuntu
sudo yum install grep # RHEL/CentOS/FedoraUsing real‑world examples helps you get comfortable with grep.
1. Search and locate packages
Example: list installed Python packages on a fresh Ubuntu system. sudo dpkg -l | grep -i python Sample output shows installed versions such as python2.7.3‑0ubuntu3.4, etc.
2. Filter file contents
Remove comment lines from an Apache configuration file.
sudo grep -v "#" /etc/apache2/sites-available/default-sslThe -v option inverts the match, printing non‑matching lines.
3. Find all mp3 files
Combine find with grep to locate Jay‑Z tracks while excluding remixes.
sudo find . -name "*.mp3" | grep -i JayZ | grep -vi "remix"4. Show line numbers around matches
Use -A and -B to display lines after or before a match.
sudo ifconfig | grep -A 4 eth0
sudo ifconfig | grep -B 2 UP5. Show context lines
The -C option prints lines before and after the matching line.
sudo ifconfig | grep -C 2 lo6. Count matches
Pipe to wc -l or use -c to count.
sudo ifconfig | grep -c inet67. Show matching line numbers
The -n option reveals the line number of each match.
sudo grep -n "main" setup.py8. Recursive search
Search all files under a directory with -r.
sudo grep -r "function" *9. Exact word match
Use -w to match whole words only.
sudo ifconfig | grep -w "RUNNING"
sudo ifconfig | grep -w "RUN"10. Search inside gzip files
zgrepworks like grep on compressed logs.
sudo zgrep -i error /var/log/syslog.2.gz11. Use extended regular expressions
egrepor grep -E enables extended regex features.
sudo grep -E "pattern"12. Fixed‑string search
fgrep(or grep -F) searches for literal strings, often with a pattern file.
sudo fgrep -f file_full_of_patterns.txt file_to_search.txtThese examples illustrate how versatile grep is for searching, filtering, counting, and automating tasks on Linux.
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.
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.
