Fundamentals 9 min read

Mastering grep: 12 Practical Linux Search Techniques

This guide walks you through twelve essential grep use‑cases on Linux, from installing the tool and listing packages to filtering files, displaying line numbers, counting matches, recursive searches, exact‑word matches, and working with compressed or regex‑based searches, all illustrated with clear command examples.

ITPUB
ITPUB
ITPUB
Mastering grep: 12 Practical Linux Search Techniques

1. Install grep

grep is pre‑installed on most Linux distributions. If it is missing, install it via the package manager:

$ sudo apt-get install grep   # Debian/Ubuntu
$ sudo yum install grep       # RHEL/CentOS/Fedora

2. List installed packages and filter for Python

Use dpkg -l to list .deb packages, then pipe the output to grep -i python to find any Python‑related entries, ignoring case.

$ sudo dpkg -l | grep -i python

3. Remove comment lines from a configuration file

To view a file without comment lines (lines containing "#"), use the -v option to invert the match.

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

4. Find all mp3 files for a specific artist while excluding remixes

Combine find with grep -i and grep -vi to locate files matching the artist name but not containing the word "remix".

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

5. Show surrounding lines with line numbers

Use -A (after) and -B (before) to display lines surrounding a match, or -C to show both sides centered on the match.

$ sudo ifconfig | grep -A 4 eth0
$ sudo ifconfig | grep -B 2 UP
$ sudo ifconfig | grep -C 2 lo

6. Count matching lines

The -c option directly returns the number of matching lines, equivalent to piping to wc -l.

$ sudo ifconfig | grep -c inet6

7. Show line numbers of matches

Use -n to prefix each matching line with its line number, useful for locating code in source files.

$ sudo grep -n "main" setup.py

8. Recursive search

The -r flag makes grep search through all sub‑directories.

$ sudo grep -r "function" *

9. Exact‑word matching

Apply -w to match whole words only, avoiding partial matches.

$ sudo ifconfig | grep -w "RUNNING"
$ sudo ifconfig | grep -w "RUN"

10. Search inside gzip‑compressed files

Use zgrep, which works like grep but reads gzip files directly.

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

11. Use extended regular expressions

Enable extended regex with -E (or use egrep) to leverage additional metacharacters.

$ sudo grep -E "pattern" file

12. Fixed‑string search with fgrep

fgrep

(or grep -F) searches for literal strings without interpreting regex metacharacters. It can read patterns from a file.

$ sudo fgrep -f file_full_of_patterns.txt file_to_search.txt

These examples illustrate how grep can be combined with other shell utilities to perform powerful text‑search tasks, automate diagnostics, and build custom scripts.

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.

TutorialregexSearchGrepcommand-line
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.