September 20, 2026
Grep for Hackers: The One Command-Line Tool Youโll Use More Than Any Exploit
If you ask ten hackers what tool they open the most, half of them will say Burp Suite, a few will say Nmap, and the honest ones will sayโฆ

By PWNXOTUS
5 min read
If you ask ten hackers what tool they open the most, half of them will say Burp Suite, a few will say Nmap, and the honest ones will say grep.
It doesn't look like much. It's just three letters and a wall of text. But once you actually understand it, you'll realize grep is quietly doing the heavy lifting behind log analysis, source code review, recon, CTFs, and even malware triage. It's the tool that turns "10,000 lines of noise" into "the 3 lines that matter."
This article is a practical, no-fluff walkthrough of grep from a hacker's point of view โ not a man-page dump, but the stuff you'll actually use.
What grep Actually Does
grep stands for Global Regular Expression Print. That name is basically the whole story: it searches through text using patterns (regular expressions) and prints the lines that match.
That's it. No magic. But when your job is finding a needle in a haystack โ a password in a config file, an IP in a 2GB log, a JavaScript endpoint hidden in minified code โ "find the lines that match a pattern" becomes one of the most powerful things you can do.
Basic syntax:
grep "pattern" filenamegrep "pattern" filenameExample:
grep "password" config.txtgrep "password" config.txtThis prints every line in config.txt that contains the word "password." Simple, but already useful โ imagine doing this across an entire leaked codebase instead of scrolling through it by hand.
The Flags That Actually Matter
Most tutorials throw 30 flags at you. Here are the ones you'll use in real hacking work, 90% of the time.
-i Case Insensitive
Attackers don't follow naming conventions. Sometimes it's Password, sometimes PASSWORD, sometimes passWORD.
grep -i "password" config.txtgrep -i "password" config.txt-r / -R Recursive Search
This is the one that turns grep into a source-code-auditing weapon. Instead of searching one file, it searches an entire directory tree.
grep -r "api_key" ./project/grep -r "api_key" ./project/Every serious hacker has run some version of this line while hunting for hardcoded secrets in a codebase.
-n Show Line Numbers
Knowing that something exists isn't enough โ you need to know where.
grep -n "eval(" script.jsgrep -n "eval(" script.jsNow you get the exact line number, which saves you from manually scrolling through a huge file to find the vulnerable eval() call.
-v Invert Match (Show What DOESN'T Match)
Underrated. Great for filtering out noise from logs.
grep -v "200 OK" access.loggrep -v "200 OK" access.logThis shows you every log line that is not a normal 200 response โ meaning it surfaces the errors, redirects, and suspicious status codes automatically.
-c Count Matches
Instead of printing every match, just count them.
grep -c "Failed password" auth.loggrep -c "Failed password" auth.logThis one line can tell you, in an instant, whether a server is under a brute-force SSH attack.
-l List Filenames Only
When you're searching across hundreds of files and just want to know which files contain a match, not the matches themselves.
grep -rl "BEGIN RSA PRIVATE KEY" /var/www/grep -rl "BEGIN RSA PRIVATE KEY" /var/www/This hunts down every file on a web server that might contain an exposed private key โ instantly.
-A, -B, -C Context Lines
Sometimes a single matching line isn't enough context.
grep -A 3 -B 3 "Unauthorized" app.loggrep -A 3 -B 3 "Unauthorized" app.log-A shows lines after the match, -B shows lines before, and -C shows both. This is how you reconstruct what happened right before and after a suspicious event in a log file.
-E Extended Regex (ERE)
Lets you use more powerful regex syntax without escaping everything with backslashes.
grep -E "admin|root|superuser" users.txtgrep -E "admin|root|superuser" users.txt-P Perl-Compatible Regex (PCRE)
The most powerful mode. Supports lookaheads, lookbehinds, and complex patterns that plain grep can't handle.
grep -P "(?<=api_key=)[a-zA-Z0-9]{32}" config.envgrep -P "(?<=api_key=)[a-zA-Z0-9]{32}" config.envThis extracts a 32-character API key value without printing the api_key= prefix โ using a lookbehind.
--color=auto
Purely for your sanity. Highlights the matched text in the output so it's not buried visually.
Real Hacker Use Cases
This is where grep stops being "a search tool" and starts being "part of your methodology."
1. Hunting for Secrets in Source Code
One of the most common things bug bounty hunters and pentesters do after cloning or downloading a repository:
grep -rniE "(api_key|secret|password|token|aws_access_key_id)" .grep -rniE "(api_key|secret|password|token|aws_access_key_id)" .This single line has found real, exploitable secrets in production codebases more times than most people would guess. Companies commit .env files, hardcode AWS keys, and leave test passwords in comments constantly.
2. Finding Hidden Endpoints in JavaScript Files
During recon, JS files often reveal API endpoints that aren't documented anywhere.
grep -oE "https?://[a-zA-Z0-9./?=_-]*" app.jsgrep -oE "https?://[a-zA-Z0-9./?=_-]*" app.jsThe -o flag only prints the matched portion (the URL itself) instead of the whole line โ perfect for building a list of endpoints to test.
3. Parsing Nmap Output for Open Ports
grep "open" nmap_scan.txtgrep "open" nmap_scan.txtCombine it further:
grep "open" nmap_scan.txt | grep -oE "^[0-9]+"grep "open" nmap_scan.txt | grep -oE "^[0-9]+"Now you've extracted just the port numbers, ready to feed into another tool.
4. Detecting Brute-Force Attempts in Auth Logs
grep "Failed password" /var/log/auth.log | wc -lgrep "Failed password" /var/log/auth.log | wc -lOr to see who is being targeted:
grep "Failed password" /var/log/auth.log | awk '{print $9}' | sort | uniq -c | sort -nrgrep "Failed password" /var/log/auth.log | awk '{print $9}' | sort | uniq -c | sort -nrThis shows a ranked list of usernames attackers are trying most โ a classic first move in incident response.
5. Searching for IOCs (Indicators of Compromise)
If you have a known-bad IP address or domain from threat intel, grep across your logs instantly tells you if you've been touched by it.
grep -r "185.220.101.5" /var/log/grep -r "185.220.101.5" /var/log/6. CTF Flag Hunting
Flags are often hidden in files, memory dumps, or binaries with a predictable format like flag{...} or CTF{...}.
grep -aroE "flag\{[^}]*\}" ./extracted_files/grep -aroE "flag\{[^}]*\}" ./extracted_files/The -a flag treats binary files as text, which matters a lot when you're grepping through memory dumps or disk images.
7. Searching Binaries for Readable Strings
Combine grep with strings for basic malware/binary triage:
strings suspicious.exe | grep -i "http"strings suspicious.exe | grep -i "http"This is often the fastest way to spot a hardcoded command-and-control URL inside a malware sample, before you even open a disassembler.
Chaining grep With Other Tools
grep rarely works alone. Its real power comes out when it's part of a pipeline.
cat access.log | grep "POST" | grep "/admin" | awk '{print $1}' | sort | uniq -c | sort -nrcat access.log | grep "POST" | grep "/admin" | awk '{print $1}' | sort | uniq -c | sort -nrRead left to right, this line:
- Reads the log file
- Filters only POST requests
- Filters only requests to
/admin - Extracts the IP address
- Counts how many times each IP appears
- Sorts by frequency
In one line, you've gone from a raw access log to "here are the IPs most aggressively hitting your admin panel." That's the grep mindset โ small, composable filters chained into something powerful.
A Quick Regex Refresher (Because grep Is Only as Good as Your Pattern)
You don't need to be a regex wizard, but knowing these basics will 10x what you can do:
Combine these and you can build patterns like matching every IPv4 address in a file:
grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" logfile.txtgrep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" logfile.txtA Handy Cheat Sheet to Keep Nearby
bash
grep -i "text" # case-insensitive search
grep -r "text" ./dir/ # recursive search in directory
grep -n "text" file # show line numbers
grep -v "text" file # invert match (exclude lines)
grep -c "text" file # count matches
grep -l "text" ./dir/* # list filenames with matches
grep -A 3 -B 3 "text" file # show 3 lines before/after match
grep -E "a|b|c" file # match multiple patterns (OR)
grep -P "regex" file # Perl-compatible regex (lookaheads etc.)
grep -o "text" file # print only the matched part
grep -a "text" binaryfile # treat binary as textgrep -i "text" # case-insensitive search
grep -r "text" ./dir/ # recursive search in directory
grep -n "text" file # show line numbers
grep -v "text" file # invert match (exclude lines)
grep -c "text" file # count matches
grep -l "text" ./dir/* # list filenames with matches
grep -A 3 -B 3 "text" file # show 3 lines before/after match
grep -E "a|b|c" file # match multiple patterns (OR)
grep -P "regex" file # Perl-compatible regex (lookaheads etc.)
grep -o "text" file # print only the matched part
grep -a "text" binaryfile # treat binary as textWhy This Matters More Than It Seems
New hackers tend to chase the flashy stuff โ exploits, zero-days, fancy frameworks. But in practice, a huge percentage of real offensive and defensive security work comes down to searching through large amounts of text quickly and precisely. Source code reviews, log analysis, malware triage, recon, incident response โ grep sits at the center of all of it.
It's not glamorous. But the difference between someone who can theoretically explain SQL injection and someone who can actually find a hardcoded database password in 40,000 lines of leaked source code in under ten seconds โ that difference is usually just knowing how to use grep well.
Learn it properly, chain it with awk, sort, uniq, and strings, and it becomes one of the sharpest tools in your entire toolkit โ free, built into every Linux box you'll ever touch, and faster than almost anything else you could reach for instead.