Web Hosting Talk







View Full Version : How to test if file is "binary" or "ascii"?


mrzippy
02-24-2004, 04:30 PM
I am wondering if it is possible to reliably test to see if any given file is a binary file.

I can use either shell or perl to do this.... any ideas?

Thanks!

hiryuu
02-24-2004, 06:01 PM
Try 'if ( -T filename )' and 'if ( -B filename )' in perl.

Riftweb
02-24-2004, 07:05 PM
Found this off google.

In shell , use the "file" command.

On most systems, Sun/Solaris being a notable exception, the result
will include the word "text" if it's an ASCII file:

FILE=/path/to/file
case `file $FILE` in
*text*) echo ASCII ;;
*) echo Binary ;;
esac

If your system doesn't consistently include the word "text", you
can add tests for the strings it does return, or use a different
magic file. (The magic file contains the strings that the file
command uses.)

Hope that helps!