Sunday, August 16, 2009

How to search for a string inside multiple text files

find starting_folder -name "*" | grep -i file_name | xargs grep -i search_string

for example:

"find . -name "*" | grep -i hobba | xargs grep -i tito"

searches for the string "tito" inside all files whose name contains the word "hobba" inside the current folder.

Note that the "-i" parameter for the "grep" command makes the search case insensitive.

2 comments:

Mohammed Hewedy said...
This comment has been removed by the author.
Mohammed Hewedy said...

Great usage of the command line.

We also can achieve the same result using the following command :

grep -iR tito *hobba*

But if we want to search files contains hobba that may be HoBBa or HobbA (i.e search files itself case sensitivity ), we should use your command. with -iname instead of -name

Thanks for your sharing of valuable information.