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.
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/Fedora2. 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 python3. 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-ssl4. 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 lo6. Count matching lines
The -c option directly returns the number of matching lines, equivalent to piping to wc -l.
$ sudo ifconfig | grep -c inet67. 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.py8. 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.gz11. Use extended regular expressions
Enable extended regex with -E (or use egrep) to leverage additional metacharacters.
$ sudo grep -E "pattern" file12. 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.txtThese examples illustrate how grep can be combined with other shell utilities to perform powerful text‑search tasks, automate diagnostics, and build custom scripts.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
