Is there a Linux utility which will return the name of a file only if it contains multiple specified strings? Not necessarily on the same line. Obviously grep can "OR" but I'm looking for "AND".


For simple things you can use grep or awk, for more complex things you will need a programming language. Probabely the best is start with a scripting language like: python, perl, php, javascript.

The grep command can be used for OR:

ls|egrep 'first_pattern|second_pattern"

Or to match both patterns:

ls|egrep "first_pattern.*second_pattern|second_pattern.*first_pattern"

To understand better how to write RegEx patterns, see: https://en.wikipedia.org/wiki/Regular_expression

Really complex parsers can be written with a parser generator like ANTLR

  • The "Or to..." solution was exactly what I was looking for although I'm not sure I understood it at first. When I looked recently I realized the significance of the '.*' meaning any number of other characters. Dec 31 '17 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.