Root to Linux: Search Files with Grep
Tags: linux, tutorial, beginners
Published: 2022-01-04 15:38:13

## 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](https://dev.to/coffeecraftcode/root-to-linux-hands-on-commands-part-1-284e) 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_

Example:
![Creating a directory and files with Linux commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odrmae8lpg7vjfl31zuu.png)
 
## 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
```
![Using grep to search a file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ul66h0ixtpjgrfsv2ad2.png)
<small>Nothing is returned</small> 

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:
```bash
A Cat Haiku
```
![Using the -i flag with grep](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkrl3rnokwrtgfh8rfht.png)

## 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:
```bash
/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
```
![Using the -r flag with grep](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgdg8m4il7yp6115a9cz.png)

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:
```bash
grep -v "meow" poem.txt
```
Output: 
```bash
A Cat Poem
```
![inverted search with -v flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyeo0s29pnuma447ysca.png)

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
```
![Searching with grep and the -l flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e68loz52mrfatzxsimb1.png)

> 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](https://www.geeksforgeeks.org/piping-in-unix-or-linux/#:~:text=A%20pipe%20is%20a%20form,program%2Fprocess%20for%20further%20processing.) 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. 
{% twitter 1475936778792148993 %}