Grep In a Directory

Last Updated on 2023-01-21

[#grep-current-directory][.inline-code]grep[.inline-code] search current directory[#grep-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 [.inline-code]grep[.inline-code] command as follows:

 $ grep “string*” *

For example:

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

[#grep-current-directory-and-subdirectories][.inline-code]grep[.inline-code] search current directory and subdirectories[#grep-current-directory-and-subdirectories]

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

 $ grep -r ‘string*’ *

For example:

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

[#grep-r-vs-R][.inline-code]grep -r[.inline-code] is different from [.inline-code]grep -R[.inline-code][#grep-r-vs-R]

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

[#grep-specific-directory][.inline-code]grep[.inline-code] search specific directory[#grep-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*’ 

For example:

[#grep-multiple-files][.inline-code]grep[.inline-code] across multiple files[#grep-multiple-files]

If you are wanting to match a search string across multiple files that you know the name of, the [.inline-code]grep[.inline-code] command takes the form:

 $ grep “string”  

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

For example:

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

 $ grep “string” *.

For example:

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-all-files][.inline-code]grep[.inline-code] search all files[#grep-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 [.inline-code]/home[.inline-code] or [.inline-code]/usr[.inline-code] or [.inline-code]/etc[.inline-code] and then run the grep command with the recursive search flag ([.inline-code]-r[.inline-code]).

[#grep-modifiers-multiple-files]Modifiers to the [.inline-code]grep[.inline-code] command across multiple files[#grep-modifiers-multiple-files]

[#grep-exclude-directories][.inline-code]grep[.inline-code] exclude directories[#grep-exclude-directories]

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

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

For example:

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

[#grep-counting-multiple-files][.inline-code]grep[.inline-code] counting across multiple files[#grep-counting-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

Read more about counting with [.inline-code]grep[.inline-code].

[#grep-ignore-case][.inline-code]grep[.inline-code] ignore case[#grep-ignore-case]

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

Read more about making [.inline-code]grep[.inline-code] case insensitive.

[#grep-return-line-number][.inline-code]grep[.inline-code] return line number[#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 [.inline-code]-n[.inline-code] flag to your command.

For example:

[#grep-show-file-name][.inline-code]grep[.inline-code] show file name[#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 [.inline-code]-l[.inline-code] flag which stands for “show the file name, not the result itself”.

[#more-on-grep]Find out more about [.inline-code]grep[.inline-code][#more-on-grep]

As always if you want to find out more about how to use the [.inline-code]grep[.inline-code] tool you can use:

 $ man grep

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

 $ grep --help

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