Fundamentals 8 min read

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.

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

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/Fedora

Using 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-ssl

The -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 UP

5. Show context lines

The -C option prints lines before and after the matching line.

sudo ifconfig | grep -C 2 lo

6. Count matches

Pipe to wc -l or use -c to count.

sudo ifconfig | grep -c inet6

7. Show matching line numbers

The -n option reveals the line number of each match.

sudo grep -n "main" setup.py

8. 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

zgrep

works like grep on compressed logs.

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

11. Use extended regular expressions

egrep

or 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.txt

These examples illustrate how versatile grep is for searching, filtering, counting, and automating tasks on Linux.

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.

Linuxtext processingShell scriptingGrep
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.