FactorPad
Build a Better Process

Linux grep Command Summary with Examples

Linux grep allows you to search for text patterns in a file.
  1. Purpose - Learn what grep is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with grep.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux grep, 39 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / grep-command.html


An ad-free and cookie-free website.


Examples of the Linux grep Command

Beginner

Learn to search for text patterns at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux grep Command Summary with Examples (3:30)

Video Script

The Command and Why You Need It

Our thirty-ninth word, or command to memorize is grep from our category Text.

grep allows you to search for text patterns in a file.

Common Linux grep Options
-options description
--color Highlight selected text in a different color
-c Count the number of occurrences
-n Print line numbers

Recall from recent videos, we've been sorting, cutting, pasting and joining files together. Now we'll advance by searching whole files of text.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The join command has over 40 options, and the arguments represents the file you'd like to read.

Like most commands, help is available with double-dash --help. And because grep is so advanced and important, I'll keep it simple here and later refer you to a helpful reading.

So why is grep an important command? Well, just like the search feature in a word processor, this Linux command is very useful for finding text patterns. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Remember back a few videos ago, we saw a misspelled word in one of our text files? I typed "thrid", instead of "third", so let's use grep to look for it. I don't recall exactly where, but it was one of the videos in the 20s, I think. So let's use grep with the --color option and in quotes type "thrid" video2*.txt

$ grep --color "thrid" video2*.txt video20.txt:This is the thrid line $ clear

There it is, in video20.txt highlighted in a different color.

Then we'll add the -c option to count the number of places it has been misspelled, and broaden the search to include all files.

$ grep --color -c "thrid" video*.txt video05.txt:1 video11.txt:0 video16.txt:0 video19.txt:0 video20.txt:1 video21.txt:0 video23.txt:0 video24.txt:0 video26.txt:0 video27.txt:0 video31.txt:2 video32.txt:2 video33.txt:0 video34.txt:0 video35.txt:0 video36.txt:0 video37.txt:0 video38a.txt:0 video38b.txt:0 video38c.txt:0 $ clear

And wow, there are more than I thought.

And last, let's throw in the -n option to include a line number, so we can get after these and tidy up, because we do like accuracy.

$ grep --color -n "thrid" video*.txt video05.txt:3:This is the thrid line video20.txt:5:This is the thrid line video31.txt:3:This is the thrid line video31.txt:10:This is the thrid line video32.txt:13:iThis is the thrid line video32.txt:14:jThis is the thrid line

Very good.

A Final Tip

Okay now you know how to use grep. And you know the syntax for commands, options and arguments.

One last tip about the grep command. So I only touched on the vast power of grep. In the Comments section find a link to a helpful page on Wikipedia on what are called Regular Expressions.

Okay, thanks for visiting today. I hope this was a helpful introduction to the grep command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Learn faster by combining this page with the video. Subscribe and follow @factorpad on Twitter for updates.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / grep-command.html


linux grep command
grep linux
linux grep
unix grep
grep unix
linux grep examples
grep examples
man grep
grep command
find grep
grep search patterns
grep regular expression
grep options
grep syntax
linux find text
grep summary
linux grep tutorial
grep usage
grep help
linux find text patterns
linux text search

A newly-updated free resource. Connect and refer a friend today.