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

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

    perl -E 'exit((-B $ARGV[0])?0:1);' file-to-test
    

    Could be used to check whenever "file-to-test" is binary. The above command will exit wit code 0 on binary files, otherwise the exit code would be 1.

    The reverse check for text file can look like the following command:

    perl -E 'exit((-T $ARGV[0])?0:1);' file-to-test
    

    Likewise the above command will exit with status 0 if the "file-to-test" is text (not binary).

    Read more about the -B and -T checks using command perldoc -f -X.

提交回复
热议问题