grep Explained With Real IT Examples
grep is the Linux command you use when you need to find matching text inside files or command output. If you work help desk, support Linux servers, poke around WSL, or get asked to “check the logs real quick,” grep is how you avoid opening a giant file and scrolling like a person who has accepted defeat.
The basic idea is simple:
grep "text to find" file.txt
That command searches file.txt for lines that contain text to find and prints the matching lines.
That is it. The power comes from using it in realistic places: logs, config files, command output, error messages, usernames, IP addresses, service names, and all the boring text that tells you what actually happened.
The short version
Start with these patterns:
grep "error" app.log
grep -i "failed" auth.log
grep -n "Listen" /etc/ssh/sshd_config
grep -r "database" /etc/myapp/
ps aux | grep nginx
journalctl -u ssh | grep "Failed password"
What those do:
grep "error" app.logsearches one file.grep -i "failed" auth.logignores uppercase/lowercase differences.grep -n "Listen" fileshows line numbers.grep -r "database" folder/searches files inside a folder recursively.ps aux | grep nginxfilters command output fornginx.journalctl -u ssh | grep "Failed password"filters SSH service logs.
If you remember nothing else, remember this: grep finds matching lines. It does not explain the whole problem for you. It just points you at the part of the haystack that might contain the needle.
Why help desk techs should learn grep
A lot of beginner Linux work is not glamorous. You are not redesigning infrastructure. You are trying to answer questions like:
- Did the user actually log in?
- Did the service throw an error?
- Is the config file using the port we think it is using?
- Did this IP address show up in the logs?
- Is the word
disabledhiding somewhere in this setting? - Did the app write anything useful before it crashed?
Without grep, beginners often do one of two things:
- Open a huge log in an editor and scroll forever.
- Copy the whole file somewhere else and make a mess.
Neither is a career-ending crime, but both waste time. grep lets you ask a focused question from the terminal.
Search one file
The simplest grep command is:
grep "failed" app.log
Example output:
2026-05-13 08:14:22 login failed for user jess
2026-05-13 08:15:03 password reset failed for user marco
That output means the matching lines contained the word failed.
Beginner note: by default, grep is case-sensitive. This matters more than people expect.
grep "failed" app.log
will match failed, but not Failed or FAILED.
Use -i when you do not care about case:
grep -i "failed" app.log
That is usually what you want when searching logs written by humans, apps, or vendors who all had different opinions about capitalization.
Show line numbers with -n
When you find a match in a config file, you often need the line number so you can inspect it or talk about it clearly.
grep -n "Port" /etc/ssh/sshd_config
Example:
14:#Port 22
31:Port 2222
The number before the colon is the line number. In this example, line 14 is commented out with #, while line 31 is the active-looking setting.
That is a real support distinction. If someone says “SSH is configured for port 22,” but the file has Port 2222 active, you just found the kind of small detail that explains a big ticket.
Search logs without opening the whole file
Imagine a user says they could not log in around 8:15. You have an auth log. Instead of opening the entire thing, search for the username:
grep -i "jess" /var/log/auth.log
Then search for common failure words:
grep -i "failed" /var/log/auth.log
grep -i "invalid" /var/log/auth.log
grep -i "denied" /var/log/auth.log
You can also combine this with tail when you only care about recent lines:
tail -n 200 /var/log/auth.log | grep -i "failed"
That says: show the last 200 lines of the auth log, then filter those lines for failed.
This is a very normal help desk pattern. You do not need to read every historical log line from last month. You need the recent evidence around the ticket.
Filter command output with a pipe
grep is often used with |, the pipe character. A pipe sends the output of one command into another command.
Example:
ps aux | grep nginx
That means:
ps auxprints running processes.grep nginxfilters that output for lines containingnginx.
Example output:
www-data 841 0.0 0.4 55248 12420 ? S 08:10 0:00 nginx: worker process
root 839 0.0 0.2 55200 6212 ? Ss 08:10 0:00 nginx: master process
This is useful when someone asks whether a service appears to be running and you are not yet ready to use the more specific service tools.
One annoying beginner moment: the grep command can match itself.
ps aux | grep nginx
might show a line like:
stetson 2451 0.0 0.0 grep nginx
That does not mean nginx is running. It means your search command contained the word nginx. Congratulations, Linux technically told the truth in the least helpful way possible.
A common workaround is:
ps aux | grep nginx | grep -v grep
The -v flag means “invert match,” so grep -v grep removes lines that contain grep.
Search a folder recursively with -r
Sometimes you do not know which file contains the setting. Use -r to search recursively through a folder:
grep -r "database_url" /etc/myapp/
Example output:
/etc/myapp/app.conf:database_url=postgres://db01/internal
/etc/myapp/backup.conf:# old database_url value removed
The output includes the file path, then the matching line.
Use this carefully on huge folders. Searching / recursively is a great way to make your terminal look busy while your laptop quietly questions your judgment.
Good beginner targets:
grep -r "Listen" /etc/apache2/
grep -r "server_name" /etc/nginx/
grep -r "timeout" /etc/myapp/
Bad beginner target:
grep -r "thing" /
If you really need broad searches later, there are better tools and flags. For beginner support work, stay scoped.
Match whole words with -w
Sometimes your search text appears inside other words. Use -w for whole-word matching:
grep -w "port" notes.txt
This matches port as a word, not important or portable.
That sounds picky, but it matters when you search config files for short terms like on, off, id, port, or user.
Show context around a match
A matching line is useful. The lines around it are often more useful.
Use -C to show context before and after the match:
grep -C 3 "ERROR" app.log
That shows 3 lines before and 3 lines after each matching line.
You can also use:
grep -B 5 "ERROR" app.log
grep -A 5 "ERROR" app.log
-B 5shows 5 lines before the match.-A 5shows 5 lines after the match.
This is handy because the actual error line may be useless by itself:
ERROR: request failed
Thanks, app. Very insightful.
The useful detail might be two lines above it:
User: jess
Endpoint: /api/upload
File size: 0 bytes
ERROR: request failed
Now you have a real clue.
Search compressed logs? Not with plain grep
On many Linux systems, older logs get compressed. You may see files like:
auth.log.1
auth.log.2.gz
auth.log.3.gz
Plain grep works on normal text files, but not directly on .gz files in a useful way.
Use zgrep for compressed logs:
zgrep -i "failed password" /var/log/auth.log.2.gz
That is not a command you need on day one, but it is a nice one to know when the incident happened yesterday and the log already rotated.
Common beginner grep mistakes
Forgetting quotes
This usually works:
grep error app.log
But for phrases, use quotes:
grep "permission denied" app.log
Without quotes, the shell treats the words separately and the command will not mean what you think it means.
Searching the wrong file from the wrong folder
Before running grep, check where you are:
pwd
ls -la
Then search the file you actually meant:
grep -i "error" ./app.log
A lot of Linux confusion is not command confusion. It is location confusion wearing a fake mustache.
Assuming no output means no problem
If grep prints nothing, it means no lines matched your exact search. It does not always mean the issue is gone.
Try different case handling:
grep -i "error" app.log
Try related words:
grep -i "fail" app.log
grep -i "denied" app.log
grep -i "timeout" app.log
Try recent logs:
tail -n 500 app.log | grep -i "error"
Be curious, not dramatic.
A practical help desk grep workflow
Here is a realistic mini-flow for checking a web app log after a user reports an upload failure.
cd /var/log/myapp
pwd
ls -la
tail -n 500 app.log | grep -i "upload"
tail -n 500 app.log | grep -i "error"
tail -n 500 app.log | grep -i "jess"
grep -C 3 -i "permission denied" app.log
What you are doing:
- Move to the log folder.
- Confirm where you are.
- Confirm the log file exists.
- Search recent lines for the feature, error words, and username.
- Pull context around the likely cause.
That is not fancy sysadmin work. It is competent support work. Competent support work fixes a surprising number of problems.
When to use grep vs opening the file
Use grep when:
- the file is large
- you know a word, username, IP, service, or error phrase
- you need a fast clue
- you are filtering command output
- you want to avoid editing a file by accident
Open the file when:
- you need to read the whole config section
- you need to make a careful edit
- the file is small enough that reading it is easier
- grep found the right area and now you need context
A good beginner habit: use grep to find the area, then use less, nano, or your editor to inspect carefully. Do not blindly edit the first line that matched. That is how “quick fix” becomes “why is the service down?”
Practice this safely
You can practice grep without touching production systems. Create a small file:
cat > support.log <<'EOF'
08:10 login ok user=jess
08:12 upload failed user=jess reason=permission denied
08:13 login ok user=marco
08:15 password reset failed user=marco
EOF
Then run:
grep "failed" support.log
grep -i "permission" support.log
grep -n "marco" support.log
grep -C 1 "permission denied" support.log
That little drill teaches the part that matters: asking better questions of text.
If you want a safer place to build this muscle, Shell Samurai gives you command-line practice without turning your actual work machine into the training dummy. Start with the text-search and log-reading style exercises, then come back to real tickets with fewer panic-tabs open.
Practice beginner Linux commands in Shell Samurai
Quick grep cheat sheet
| Goal | Command |
|---|---|
| Search one file | grep "error" app.log |
| Ignore case | grep -i "error" app.log |
| Show line numbers | grep -n "Port" sshd_config |
| Search a folder | grep -r "timeout" /etc/myapp/ |
| Exclude matches | grep -v "debug" app.log |
| Show context | grep -C 3 "ERROR" app.log |
| Filter process output | ps aux | grep nginx |
| Search compressed logs | zgrep "failed" auth.log.2.gz |
grep is not a personality trait. It is a small, reliable tool for finding text under pressure. Learn the basic flags, practice on fake logs, and you will be a lot less helpless the next time a Linux box gives you a giant file and a vague error message.
Practice This in a Real Terminal
Shell Samurai gives you safe Linux missions so the commands actually stick. Chapter 1 is free; the full practice path is a one-time purchase, not another subscription.