Introduction
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.
Prerequisites
- 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
book
and 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.
Inside the story.txt
file add this text:
A Cat Haiku
In the morning
You sleep despite my meow
I stand on your face
Inside the poem.txt
file add this text:
A Cat Poem
Meow meow meow
Meow meow meow meow
Meow meow
Searching Within a File
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:
bash
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:
bash
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 --ingnore-case
.
bash
grep -i cat story.txt
The output should now return the correct line that contains the pattern.
Output:
A Cat Haiku
Searching Recursively
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 --recursive
In this example we will search for the word meow in the book
directory.
The command will use grep
, the -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
Output:
/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.
Invert search with -v
Flag
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 poem.txt
file.
While in the book
directory type:
grep -v "meow" poem.txt
Output:
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.
Search for Specific File Types
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.
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.
Navigate to the book
directory.
Type:
bash
grep -l meow *.txt
Output:
bash
poem.txt
story.txt
Note: The * in Linux stands for "zero or more characters". In the example it looks for all files with the
.txt
characters.
Conclusion
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 grep
type: man grep
in your terminal. This will list the General Commands Manual for grep
.
If you would like to see some of these commands as visual content, check out my post where I share my GoodNotes.
Top comments (7)
I have a niggle for your otherwise very nicely written piece.
You mention the
-l
flag:What
-l
actually means is it only print the names of files which contain the pattern you specify.In addition, the GNU grep manpage says that "The scanning will stop on the first match." - which at first glance might imply that you only find the first file in the files being examined. To be precise, however, it means that for each file checked
grep
will stop checking that file if it has a match for the pattern.Thanks for the correction and additional information 😄
To learn more about where
grep
came from check out this great YouTube video by Computerphile.how to search inside files
for example i want to found word "garden" in files with extension end with .xml ?
There are a lot of different answers to this depending on how much information you have before searching.
If you know what folder the files are in you can use the
-r
flag like the example in this article.If you are not sure what folder to search in, I would try some of the commands suggested here: stackoverflow.com/questions/169568...
good job
im a fundamental in linux