How can I know if a file is a binary file?
For example, compiled c file.
I want to read all files from some directory, but I want ignore binary files.
cat+grepAssuming binary means the file containing NULL characters, this shell command can help:
(cat -v file.bin | grep -q "\^@") && echo Binary || echo Text
or:
grep -q "\^@" <(cat -v file.bin) && echo Binary
This is the workaround for grep -q "\x00", which works for BSD grep, but not for GNU version.
Basically -v for cat converts all non-printing characters so they are visible in form of control characters, for example:
$ printf "\x00\x00" | hexdump -C
00000000 00 00 |..|
$ printf "\x00\x00" | cat -v
^@^@
$ printf "\x00\x00" | cat -v | hexdump -C
00000000 5e 40 5e 40 |^@^@|
where ^@ characters represent NULL character. So once these control characters are found, we assume the file is binary.
The disadvantage of above method is that it could generate false positives when characters are not representing control characters. For example:
$ printf "\x00\x00^@^@" | cat -v | hexdump -C
00000000 5e 40 5e 40 5e 40 5e 40 |^@^@^@^@|
See also: How do I grep for all non-ASCII characters.