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

后端 未结 13 910
走了就别回头了
走了就别回头了 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:38

    grep

    Assuming binary means file containing non-printable characters (excluding blank characters such as spaces, tabs or new line characters), this may work (both BSD and GNU):

    $ grep '[^[:print:][:blank:]]' file && echo Binary || echo Text
    

    Note: GNU grep will report file containing only NULL characters as text, but it would work correctly on BSD version.

    For more examples, see: How do I grep for all non-ASCII characters.

提交回复
热议问题