Unix Command to List files containing string but *NOT* containing another string

后端 未结 6 1724
春和景丽
春和景丽 2021-01-30 22:01

How do I recursively view a list of files that has one string and specifically doesn\'t have another string? Also, I mean to evaluate the text of the files, not the filenames.

6条回答
  •  醉梦人生
    2021-01-30 22:49

    Here is a more generic construction:

    find . -name  -print0 | xargs -0 grep -Z -l  | xargs -0 grep -L 
    

    This command outputs files whose name matches (adjust find predicates as you need) which contain , but do not contain .

    The enhancements are:

    • It works with filenames containing whitespace.
    • It lets you filter files by name.

    If you don't need to filter by name (one often wants to consider all the files in current directory), you can strip find and add -R to the first grep:

    grep -R -Z -l  | xargs -0 grep -L 
    

提交回复
热议问题