Operations 9 min read

Mastering grep: 12 Powerful Ways to Search and Filter on Linux

This guide walks you through installing grep and demonstrates twelve practical techniques—including searching packages, filtering files, finding media, showing context lines, counting matches, recursive searches, exact word matches, and handling compressed files—so you can harness its full power on Linux systems.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering grep: 12 Powerful Ways to Search and Filter on Linux

grep is a powerful, pre‑installed file pattern search tool on every Linux distribution; if it is missing, install it with sudo apt-get install grep (Debian/Ubuntu) or sudo yum install grep (RHEL/CentOS/Fedora).

1. Search and find files

List installed packages and filter for Python entries:

sudo dpkg -l | grep -i python

The output shows package names, versions, and descriptions.

2. Search and filter files

Remove comment lines from an Apache configuration file:

sudo grep -v "#" /etc/apache2/sites-available/default-ssl

The -v option inverts the match, printing non‑matching lines.

3. Find all mp3 files

Combine find and grep to locate JayZ mp3 files while excluding remixes:

sudo find . -name "*.mp3" | grep -i JayZ | grep -vi "remix"

4. Show line numbers before/after matches

Use -A (after) and -B (before) to display surrounding lines:

sudo ifconfig | grep -A 4 eth0
sudo ifconfig | grep -B 2 UP

5. Show surrounding lines

The -C option prints lines on both sides of a match (centered):

sudo ifconfig | grep -C 2 lo

6. Count matches

Count matching lines directly with -c:

sudo ifconfig | grep -c inet6

7. Show matching line numbers

When debugging, -n reveals the line number of each match:

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 compressed files

Search inside gzip files with zgrep, which shares grep’s options:

sudo zgrep -i error /var/log/syslog.2.gz

11. Search with regular expressions

egrep

(or grep -E) enables extended regular expressions for more complex patterns:

sudo grep -E "pattern"

12. Fixed string search

fgrep

(or grep -F) searches for fixed strings; it can read patterns from a file:

sudo fgrep -f file_full_of_patterns.txt file_to_search.txt

Beyond one‑line commands, grep can be placed in cron jobs or shell scripts for automation; explore its man page to craft custom expressions for any need.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellSysadminGrepcommand-linetext search
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.