Let’s grep together!
If I’m being honest, I’ve always thought of grep as that cool kid in the Unix playground who I know is powerful but… I’ve never really gotten to know. Anytime I need it, I find myself Googling “grep search file for text” like it’s my first day in tech.
But today? We’re fixing that. You and I are about to sit down and really understand grep. The goal? To get confident enough to craft the right command for any use case—without side-eying Stack Overflow or ChatGPT every 2 minutes.
What is grep?
A quick google search what what grep
stands for:
Grep, short for “global regular expression print”, is a command used for searching and matching text patterns in files contained in the regular expressions
Alright, cool! but what does that mean? I did more digging to dumb it down to a more digestible form for me. Here's my summary:
- it's a search tool
- it can be used to search in files e.g csv, log files etc, path or a command that prints an output
- it does this by reading the file or input streams
line by line
Okay easy enough to start. let's continue.
Basic anatomy of a grep command
grep [OPTIONS] PATTERN [FILE...]
Example
grep "ERROR" server.log
This prints all lines in server.log containing “ERROR”.
[OPTIONS]
With grep, you can specify how the results are displayed with the use of options. Examples are:
-i
This Ignores case e.ggrep -i "error" server.log
-n
Show line numbers e.ggrep -n "ERROR" server.log
-v
Invert match e.ggrep -v "DEBUG" server.log
Show every line that does not contain DEBUG-r
Search recursively in directories e.ggrep -r "TODO" ./src
-c
Count matching lines e.ggrep -c "ERROR" server.log
-l
List only filenames with matches e.ggrep -l "ERROR" *.log
Quick recap
We have learned how to use grep to search files or directories for matching keywords using various options. Notice how you always start with the word grep followed by an option (if you require it) then the search word and finally the file or the path.
Exercises
Let’s put some of what we’ve learned into practice.
Searching logs is a huge part of my day-to-day as an engineer. While some teams use third-party tools like Sentry for error monitoring or Logz.io for log aggregation and analysis, there are plenty of times when you don’t have a fancy UI and need to get your hands dirty with raw log files.
At a past role with Logz.io, we had a weekly rotation where developers reviewed logs, identified recurring or critical errors, and raised tickets for the next sprint. In tools like Logz.io, it was easy to search by request IDs, field names, or exception types. With grep, we can achieve similar results right from the terminal.
Head on to your terminal, create a file named app.log
add the following content
cat > app.log <<EOL
2025-07-05 10:00:01 INFO Starting application
2025-07-05 10:05:23 ERROR Failed to connect to DB
2025-07-05 10:10:45 DEBUG User login attempt
2025-07-06 08:15:12 ERROR OutOfMemoryException
EOL
Challenge
- Find every line that contains the word ERROR
- Search for errors on a specific date e.g
2025-07-05
- Filter out DEBUG logs
tip: inverse
- You have a logs directory
/var/logs/
search logs files recursively for the wordOutOfMemoryException
Good job so far! I hope you enjoyed this practice as much as I did. Remember, grep isn’t limited to searching text files—it can search for matching keywords in any readable input stream. That includes:
- virtually any file type (as long as it’s text-based),
- and even the output of other commands, like system utilities.
Searching outputs of other commands
One of the coolest things about grep is that it doesn’t just work with files—it can also search the output of other commands. This is super handy when you’re dealing with system commands that dump a lot of information.
Let's look at some examples using git
The syntax is: "the command" + pipe symbol + grep + keyword
here's an example:
git log | grep 0e54043f758418b76db2b9ca44e2deaf4b0feb75
This will print that commit if it exists in git.
There are so many fun ways to search your Git commit history using grep. You can look for commits by a specific author, within a specific date range, or even filter by branch names. With the -E option, you can take it up a notch by searching for multiple keywords at once using extended regular expressions.
git log | grep -E "bugfix|refactor"
git log --author="authorName" --oneline | grep -E "hotfix|rollback"
Thats all the grep'ng for today! I hope you enjoyed it as much as I did!
Final thoughts
We’ve covered a lot today, and like any skill, it’ll take practice for it to stick. Keep experimenting, and soon crafting grep commands will feel like second nature.
Ready to make it muscle memory? Let’s grep again soon. 😉