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

后端 未结 13 924
走了就别回头了
走了就别回头了 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 17:48

    Going off Bach's suggestion, I think --mime-encoding is the best flag to get something reliable from file.

    file --mime-encoding [FILES ...] | grep -v '\bbinary$'
    

    will print the files that file believes have a non-binary encoding. You can pipe this output through cut -d: -f1 to trim the : encoding if you just want the filenames.


    Caveat: as @yugr reports below .doc files report an encoding of application/mswordbinary. This looks to me like a bug - the mime type is erroneously being concatenated with the encoding.

    $ for flag in --mime --mime-type --mime-encoding; do
        echo "$flag"
        file "$flag" /tmp/example.{doc{,x},png,txt}
      done
    --mime
    /tmp/example.doc:  application/msword; charset=binary
    /tmp/example.docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary
    /tmp/example.png:  image/png; charset=binary
    /tmp/example.txt:  text/plain; charset=us-ascii
    --mime-type
    /tmp/example.doc:  application/msword
    /tmp/example.docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
    /tmp/example.png:  image/png
    /tmp/example.txt:  text/plain
    --mime-encoding
    /tmp/example.doc:  application/mswordbinary
    /tmp/example.docx: binary
    /tmp/example.png:  binary
    /tmp/example.txt:  us-ascii
    

提交回复
热议问题