• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Grep In a Directory

Thumbnail for Philip WilkinsonPhilip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Published: 12/1/2023

About Terminus

grep search current directory

When you want to search in all the files of the current directory, regardless of their name or extension, you can use the wildcard character after your grep command as follows:

 $ grep “string*” *

Run in Warp

For example:

Thumbnail for

Note that this command does not search in subdirectories. Instead, it tells you that model1 and model2 are directories and that they are not searched. 

grep search current directory and subdirectories

To search within the current directory and all subdirectories, you can use the recursive -r flag as follows:

 $ grep -r ‘string*’ *

Run in Warp

For example:

Thumbnail for

As you can see,  instead of stating that model1 and model2 are subdirectories, they have been searched and two results have appeared in files within each.

grep -r is different from grep -R

Note that the -r flag is slightly different from  the -R flag. While -r searches all files that are present in the current directory and all subdirectories, -R will also follow symbolic links to go to the original file.

grep search specific directory

If you want to recursively search a specific directory instead of the current one, you can replace the wildcard character with the name of the directory you want to search:

 $ grep -r  ‘string*’ 

Run in Warp

For example:

Thumbnail for

grep across multiple files

If you are wanting to match a search string across multiple files that you know the name of, the grep command takes the form:

 $ grep “string”  

Run in Warp

After the string you want to match, you specify the individual files separated by a space character.

For example:

Thumbnail for

If you don’t know the actual file names but instead you know the file extension, you can use the wildcard character (*) to specify all files with the given extension. This command would take the form:

 $ grep “string” *.

Run in Warp

For example:

Thumbnail for

Which searchers all files in the current directory with the given file extension. Note that this could also be used to search all files with the same name but different extensions by changing where the wildcard character appears.

grep search all files

To search all files, you can run the commands identified above but from the root of your system. This is not recommended as you would get the results from folders that aren’t relevant to your search, such as your configuration settings. Instead, navigate to the root of where would be useful to search, such as /home or /usr or /etc and then run the grep command with the recursive search flag (-r).

Modifiers to the grep command across multiple files

grep exclude directories

In some cases you may want to exclude certain directories from your search. In this case you add the --exclude-dir flag to the command:

 $ grep -r --exclude-dir= “string” 

Run in Warp

For example:

Thumbnail for

This can also be modified to take a list of directories by replacing dir with {dir1, dir2}.

grep counting across multiple files

To match across multiple files and count the occurrences of the pattern you are searching for, you can use the following command, which will print out the occurrences as well as the file they appear in:

 $ grep -0 “string”   | cut -d ‘:’ -f 1 | uniq -c

Run in Warp

Read more about counting with grep.

grep ignore case

The grep command is case sensitive. To ignore the case you can use the -i flag. For example, when searching for the hello string, the grep command will also match Hello and hellO within a file.

Read more about making grep case insensitive.

grep return line number

When matching across multiple files you might want to print off the line number along with the output. In that case you can add the -n flag to your command.

For example:

Thumbnail for

grep show file name

In some cases, given that a lot of files will be searched which may contain multiple matches, you only want to print out the file name. For this you can add the -l flag which stands for “show the file name, not the result itself”.

Find out more about grep

As always if you want to find out more about how to use the grep tool you can use:

 $ man grep

Run in Warp

Which will print out all the options with explanations. Or:

 $ grep --help

Run in Warp

Which will print out a short page of all the available options.

Written by

Thumbnail for Philip WilkinsonPhilip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Filed Under

Related Articles

Grep Multiple Strings

How to filter lines and extract specific information from the output of commands or text files based on string patterns and regular expressions with grep.

Grep
Thumbnail for Spencer EvansSpencer Evans

How To Filter The Output of Commands

Learn how to filter and format the output of commands and logs using the grep, awk, uniq, head, and tail commands.

LinuxGrep
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to Make Grep Case Insensitive

By default, grep is case sensitive

Grep
Thumbnail for Jessica WangJessica Wang

Grep Across Multiple Lines

Guide on several cases of using grep across multiple lines

Grep
Thumbnail for Philip WilkinsonPhilip Wilkinson

Exclude With Grep

Excluding unwanted key terms or directories when using grep

Grep
Thumbnail for Razvan LudosanuRazvan Ludosanu

Grep Count

Efficiently count lines or occurrences in a file.

Grep
Thumbnail for Philip WilkinsonPhilip Wilkinson

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null