Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep Search all files in directory for string1 AND string2

Tags:

linux

grep

shell

How can I make use of grep in cygwin to find all files that contain BOTH words.

This is what I use to search all files in a directory recursively for one word:

grep -r "db-connect.php" .

How can I extend the above to look for files that contain both "db-connect.php" AND "version".

I tried this: grep -r "db-connect.php\|version" . but this is an OR i.e. it gets file that contain one or the other.

Thanks all for any help

like image 843
Abs Avatar asked Nov 25 '10 10:11

Abs


People also ask

How do I use grep to search all files in a directory?

To Search Subdirectories To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.

How do I grep all files in a directory recursively?

Recursive Search To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.

Can grep search multiple files?

Grep is a powerful utility available by default on UNIX-based systems. The name stands for Global Regular Expression Print. By using the grep command, you can customize how the tool searches for a pattern or multiple patterns in this case. You can grep multiple strings in different files and directories.


1 Answers

grep -r db-connect.php . | grep version
like image 176
moinudin Avatar answered Nov 01 '22 17:11

moinudin