Grep is a commonly used command in Linux (or Unix) to search through 1 or more files for a pattern or word.
Fun fact: grep is short for Global regular expression print(g/re/p). It was a command used in a simple Unix text editor called
ed(pronounced ee dee). It would print all the lines matching a certain pattern.
There are many ways to use the grep command. In this tutorial you will use the grep command to search within files and directories and print file types that contain a certain string and more.
- You can use a Linux shell or any Unix terminal like the one found on MacOS. If you do not have a Linux distro set up, you can use an online command shell to practice these commands. For this tutorial I used the terminal found on MacOS.
- Create a directory on your computer called
bookand add 2 text files named story.txt and poem.txt. We will use these files throughout the tutorial. If you would like to practice the Linux commands to create a directory, files, and add text please see Part 1 and 2 of my Hands-on Commands tutorial.
story.txt file add this text:
A Cat Haiku
In the morning
You sleep despite my meow
I stand on your face
poem.txt file add this text:
A Cat Poem
Meow meow meow
Meow meow meow meow
One way to use the grep command is to search for a word in a specific file. In this section you will use the grep command to search for the word cat in the story.txt file.
In your terminal navigate to your
book directory with the
cd command then type:
grep cat story.txt
This command looks in the
story.txt file for a pattern that matches
cat. You will see in the terminal that it does not return anything. There is a word cat in the story.txt file but it is capitalized.
The grep command is case sensitive. To return the line in the text file that matches the pattern cat you could type:
grep Cat story.txt
Yet, you may not know the case of a pattern you are looking for in a file. To search for a word or pattern that is case insensitive you can use the
-i flag. The
-i flag is shorthand for
grep -i cat story.txt
The output should now return the correct line that contains the pattern.
A Cat Haiku
When you want to search through multiple files or you are unsure what file contains a pattern you can search recursively through a directory with grep and the
-r flag. The -r flag is shorthand for
In this example we will search for the word meow in the
The command will use
-r flag, the pattern/string you want to search for, and the path of the directory to search in.
In your terminal type:
grep -r "meow" /Users/cmgorton/Desktop/book
/Users/cmgorton/Desktop/book/poem.txt:Meow meow meow /Users/cmgorton/Desktop/book/poem.txt:Meow meow meow meow /Users/cmgorton/Desktop/book/poem.txt:Meow meow /Users/cmgorton/Desktop/book/story.txt:You sleep despite my meow
You can see that the command returned every line that matched the pattern in both the poem and story text file.
Before, we used grep to find all the successful matches of a word in a file. You can use the
-v flag along with grep to invert the search. The
-v flag is shorthand for
--invert-match. This means the search will return all non-matching lines in a file the pattern you are searching for.
In this example we will look for all the lines that do not match the word "meow" in the
While in the
book directory type:
grep -v "meow" poem.txt
A Cat Poem
You can now try to do the same inverted search in the
story.txt file and see what the results would be.
When we searched through the previous files we knew that they contained the word meow. However, if you were unsure which files contained the word you could search for a specific file type with the
-l flag is shorthand for--files-with-matches`. It will search only the names of files containing selected lines. For this example you will search through the file type .txt to see which files contain the word meow.
Note: This command will search through all files in the current working directory.
Note: The * in Linux stands for "zero or more characters". In the example it looks for all files with the
In this tutorial you explored a few of the ways to use
grep to search through files and directories for a pattern.
There are a lot of other flags you can use with
grep. You can also use pipes to pipe the output of a command through
grep to filter out information.
To see all of the options you can use with
man grep in your terminal. This will list the General Commands Manual for
If you would like to see some of these commands as visual content, check out my post where I share my GoodNotes.
Christina GortonToday I have been learning more about the grep command.
I will write a new blog post sometime this week but until then enjoy my horrible handwriting on the iPad with these notes on some of the ways to use grep.21:08 PM - 28 Dec 2021