Skip to content

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

csv
grep [OPTIONS] PATTERN [FILE...]

Example

pgsql
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:

  1. -i This Ignores case e.g grep -i "error" server.log
  2. -n Show line numbers e.g grep -n "ERROR" server.log
  3. -v Invert match e.g grep -v "DEBUG" server.log Show every line that does not contain DEBUG
  4. -r Search recursively in directories e.g grep -r "TODO" ./src
  5. -c Count matching lines e.g grep -c "ERROR" server.log
  6. -l List only filenames with matches e.g grep -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

pgsql
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

  1. Find every line that contains the word ERROR
  2. Search for errors on a specific date e.g 2025-07-05
  3. Filter out DEBUG logs tip: inverse
  4. You have a logs directory /var/logs/ search logs files recursively for the word OutOfMemoryException

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:

bash
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.

bash
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. 😉

All rights reserved.