Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep only text files

Tags:

grep

bash

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

like image 202
stefcud Avatar asked Mar 21 '12 14:03

stefcud


People also ask

How do I grep a text file in Linux?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do I grep text in a folder?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename. In the example below, we also added the -w operator to show whole words, but the output form is the same.

How do I filter text using grep?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".


2 Answers

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.
like image 56
kev Avatar answered Oct 11 '22 05:10

kev


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 {} ";"  
like image 28
user unknown Avatar answered Oct 11 '22 05:10

user unknown