Mastering grep: 12 Powerful Ways to Search and Filter Files on Linux
This guide walks you through installing grep and demonstrates twelve practical techniques—from simple pattern searches and file filtering to recursive directory scans and compressed‑file queries—empowering you to efficiently locate and manipulate text across Linux systems.
Ever needed to find a specific string or pattern in files but didn’t know where to start? Let grep help you.
grep is a powerful pattern‑search tool pre‑installed on every Linux distribution. If it’s missing, install it with your package manager:
sudo apt-get install grep # Debian/Ubuntu
sudo yum install grep # RHEL/CentOS/Fedora
1. Search and locate packages
Example: list installed packages and filter for Python:
sudo dpkg -l | grep -i python
The output shows package name, version, and description.
2. Filter file contents
Remove comment lines (starting with #) from an Apache config file:
sudo grep -v "#" /etc/apache2/sites-available/default-ssl
3. Find all MP3 files by artist
Combine find with grep to locate Jay‑Z tracks while excluding remixes:
sudo find . -name "*.mp3" | grep -i JayZ | grep -vi "remix"
4. Show surrounding lines with line numbers
Use -A (after) and -B (before) to display context around a match:
sudo ifconfig | grep -A 4 eth0
sudo ifconfig | grep -B 2 UP
5. Show centered context
The -C option prints lines before and after the match:
sudo ifconfig | grep -C 2 lo
6. Count matching lines
Count occurrences directly with -c (equivalent to piping to wc -l):
sudo ifconfig | grep -c inet6
7. Display line numbers of matches
When debugging, -n shows the line number where the pattern appears:
sudo grep -n "main" setup.py
8. Recursive search
Search all files under the current 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 but on compressed logs:
sudo zgrep -i error /var/log/syslog.2.gz
11. Use extended regular expressions
egrep(or grep -E) enables advanced regex features for source‑code searches:
sudo grep -E "pattern" file.c
12. Fixed‑string search
fgrep(or grep -F) searches for literal strings without interpreting regex metacharacters:
sudo fgrep -f patterns.txt target.txt
These examples illustrate just a fraction of grep’s capabilities; you can embed them in scripts, cron jobs, or combine them with other tools to automate complex text‑processing tasks.
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.
