How to check if the file is a binary file and read all the files which are not?

后端 未结 13 921
走了就别回头了
走了就别回头了 2020-12-05 16:58

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.

13条回答
  •  感动是毒
    2020-12-05 17:33

    cat+grep

    Assuming 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.

提交回复
热议问题