Created by rakib_Linked to 58.8m issues across 205 teams
Here's how to find all files containing a specific text (string) on Linux.
First, open the terminal and type the following command:
grep -rnw '/path/to/somewhere/' -e 'pattern'
The command has several flags that you can use to customize your search:
-r
or -R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the searchYou can also use the --exclude
, --include
, --exclude-dir
flags to make your search more efficient. For example, if you want to search only files with the .c or .h extensions, you can use the following command:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
If you want to exclude files with the .o extension, use this command:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
You can also exclude one or more directories from your search using the --exclude-dir
parameter. For example, this command will exclude the dirs dir1/
, dir2/
and all of them matching *.dst/
:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"