Find files containing multiple strings

前端 未结 4 740
悲&欢浪女
悲&欢浪女 2021-01-20 08:26

I use a command to recursively find files containing a certain string1:

find . -type f -exec grep -H string1 {} \\;

I need to

4条回答
  •  不思量自难忘°
    2021-01-20 08:47

    You can chain your actions and use the exit status of the first one to only execute the second one if the first one was successful. (Omitting the operator between primaries defaults to -and/-a.)

    find . -type f -exec grep -q 'string1' {} \; -exec grep -H 'string2' {} \;
    

    The first grep command uses -q, "quiet", which returns a successful exit status if the string was found.

提交回复
热议问题