Grep text files only

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

This is a good decision? to search for TEXTSEARCH recursively in text files only

+40
bash grep
Mar 21 2018-12-21T00:
source share
2 answers

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

 $ grep -rI "TEXTSEARCH" . 
  • -I Process the binary as if it did not contain the corresponding data; this is equivalent to the --binary-files=without-match option.
  • -r Read all files in each directory, recursively; this is equivalent to the -d recurse .
+162
Mar 21 2018-12-12T00:
source share

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

 find . -type f -exec bash -c "file -bi {} | grep -q text" \; -exec grep TEXTSEARCH {} ";" 
+3
Mar 22 '12 at 9:20
source share



All Articles