grep only text files

BashGrep

Bash Problem Overview


find . -type f | xargs file | grep text | cut -d':' -f1 | xargs grep -l "TEXTSEARCH" {}

it's a good solution? for find TEXTSEARCH recursively in only textual files

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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 {} ";" 

Solution 3 - Bash

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" *

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstefcudView Question on Stackoverflow
Solution 1 - BashkevView Answer on Stackoverflow
Solution 2 - Bashuser unknownView Answer on Stackoverflow
Solution 3 - BashMark BView Answer on Stackoverflow