Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

With Busybox, how do you search for an expression within a bunch of files recursively through a bunch of directories, but only look through text files?

We don't know what the file's suffix is going to be; it could be .sh, it could be nothing, it could be something else. I was considering somehow basing the search on encoding although I am not quite sure what the encoding would be either.

I've tried busybox grep -r but it searches through binary files too, which wastes a lot of time.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

grep supports the option --binary-files to treat binary files as binary, text or without-match. The last option should skip binary files. It is equivalent to grep -I. I did a test and it seems Busybox's grep supports -I to skip binary files.

grep is not "magical" in determining if a file is binary or text: it simply checks the first few bytes to see if they seem to be text or not and assumes the same for the rest of the file (according to man grep).


Sidenotes: one could emulate this by e.g. comparing if head -1 myfile of a file equals head -1 myfile | strings. If file is available it is even more reliant, but it is not in Busybox, and most likely not on the system if Busybox is primarily used.

share|improve this answer
add comment

What is the difference between a binary file with the struct

struct MyBin
{
    byte a;
    byte b;
    byte c;
}

with the values

myBin.a = 70;
myBin.b = 111;
myBin.c = 111;

And a text file with the text Foo?

All a text file is, is a binary file that you interpret using special look up codes called Character Encodings (ASCII, UTF-8, ect...). So there is no easy way to tell "Binary Files" apart from "Text Files".

There may be a way to exclude files that have the execute bit set, or only search files under a file-size (I doubt your text file is over 1 MB) but I do not have enough knowledge on how to filter the grep results to give a example of how to do it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.