grep only text files

前端 未结 3 827
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 19:58
find . -type f | xargs file | grep text | cut -d\':\' -f1 | xargs grep -l \"TEXTSEARCH\" {}

it\'s a good solution? for find TEXTSEARCH recursively in o

相关标签:
3条回答
  • 2021-01-30 20:20

    If you know what the file extension is that you want to search, then a very simple way to search all *.txt files from the current dir, recursively through all subdirs, case insensitive:

    grep -ri --include=*.txt "sometext" *

    0 讨论(0)
  • 2021-01-30 20:22

    Another, less elegant solution than kevs, is, to chain -exec commands in find together, without xargs and cut:

    find . -type f -exec bash -c "file -bi {} | grep -q text" \; -exec grep TEXTSEARCH {} ";" 
    
    0 讨论(0)
  • 2021-01-30 20:28

    You can use the -r(recursive) and -I(ignore binary) options in grep:

    $ grep -rI "TEXTSEARCH" .
    
    • -I Process a binary file as if it did not contain matching data; this is equivalent to the --binary-files=without-match option.
    • -r Read all files under each directory, recursively; this is equivalent to the -d recurse option.
    0 讨论(0)
提交回复
热议问题