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