Find files containing multiple strings

前端 未结 4 739
悲&欢浪女
悲&欢浪女 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 09:10

    with GNU grep

    grep -rlZ 'string1' | xargs -0 grep -l 'string2'
    


    from man grep

    -r, --recursive

    Read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.

    -Z, --null Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, grep -lZ outputs a zero byte after each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be used with commands like find -print0, perl -0, sort -z, and xargs -0 to process arbitrary file names, even those that contain newline characters.

提交回复
热议问题