How to find files containing specific string in Linux?

I want to find all the files which contain a specific string using Linux terminal.

You can use grep to list out all the files contianing the string you are searching for.

An example syntax is given below.

grep -rnw "/path/to/folder" -e "your_search_string"

-r - > recursive,
-n -> Shows line number
-w - > it matches the full word

If you want to just list of the file names, you can using the l flag.

grep -rl "/path/to/folder" -e "your_search_string"

lets say you want to exclude some file during search. Then you can user th --exclude parameter. for example, if you want to exclude all the .txt file, you can use the following sysntax.

grep --exclude=*.txt -rnw "/path/to/folder" -e "your_search_string"

You can also useack utitlity. Some systems might not have this installed. You can install ack directly from respective package managers.

ack 'your_search_string'

The above syntax would search recursively and list all the files contianing your string.

1 Like