New answers tagged


perl -e'chomp(my$f=<>);print "binary$/" if -B $f;print "text$/" if -T _' will do it. See documentation for -B and -T (search in that page for the string The -T and -B switches work as follows).


Categories are arbitrary. Before answer how to make a classification, you need a (strict) definition. In order to have a definition, you need a purpose. So, what do you want to do with that classification? If you want to select ascii/binary in FTP, it's important do not transfer a binary file as ascii (or it will be corrupted). So you shuld test if the ...


If you like the heuristic used by GNU grep, you could use it: isbinary() { LC_MESSAGES=C grep -Hm1 '^' < "${1-$REPLY}" | grep -q '^Binary' } It searches for NUL bytes in the first buffer read from the file (a few kilo-bytes for a regular file, but could be a lot less for a pipe or socket or some devices like /dev/random). In UTF-8 locales, it also ...


Another approach would be to use isutf8 from the moreutils collection. It exits with 0 if the file is valid UTF-8 or ASCII, or short circuits, prints an error message (silence with -q) and exits with 1 otherwise.


file has an option --mime-encoding that attempts to detect the encoding of a file.  $file --mime-encoding Documents/poster2.pdf Documents/poster2.pdf: binary  $file --mime-encoding projects/linux/history-torvalds/Makefile projects/linux/history-torvalds/Makefile: us-ascii  $file --mime-encoding graphe.tex Dgraphe.tex: us-ascii  $file --mime-encoding ...


You could try determining if iconv can read the file. This is less performing than file (which just reads a couple bytes from the beginning), but will give you more reliable results: ENCODING=utf-8 if iconv --from-code="$ENCODING" --to-code="$ENCODING" your_file.ext > /dev/null 2>&1; then echo text else echo binary fi This makes iconv ...


If you ask file for just the mime-type you'll get many different ones like text/x-shellscript, and application/x-executable etc, but I imagine if you just check for the "text" part you should get good results. Eg (-b for no filename in output): file -b --mime-type filename | sed 's|/.*||'


You can write a script that calls file, and use a case-statement to check for the cases you are interested in. For example #!/bin/sh case $(file "$1") in (*script*|*\ text|*\ text\ *) echo text ;; (*) echo binary ;; esac though of course there may be many special cases which are of interest. Just checking strings on a copy of libmagic, I ...


The Ó is probably why file on your Linux box is saying ISO-8859 text, but the reason why the file output is different is because your Linux box and your AIX box have different magic databases for file to work with. AIX almost certainly has an ancient, obsolete magic db. Linux machines tend to have reasonably up-to-date ones with identifier sequences for ...

Top 50 recent answers are included