There are already two questions on this topic here, but both address errors in their scripts rather than the actual title.

I want to develop a program that obtains some basic information about a file, depending on its type. I want to distinguish between directories, text and binary data.

So far I am using file:

case "$(file --dereference $arg)" in
  *directory) ls -l --color=auto --almost-all --human-readable --group-directories-first --file-type --dereference-command-line "$@";;
  *text*|*JSON*) bat --style header "$@";;

Which mostly works, but as you can see, I already had to add an exception for JSON as file identifies that as JSON data with no mention of text. The problem is that there are more exceptions, and I'd prefer to not add them all individually.

Is there a way to obtain more general information on the content type from file or perhaps another standard program?


After some experimentation, I found a decent solution myself:

case "$(file --dereference --mime $arg)" in
 *inode/directory*) ls -l --color=auto --almost-all --human-readable --group-directories-first --file-type --dereference-command-line "$@";;
 *binary) ;;
 *) bat --style header "$@";;

It essentially reverses the approach - file --mime outputs a charset, which is binary for all non-text files. Then it treats everything which is not a binary as text, which seems to work well.

If you don't care about directories - they are also classified as binaries, so then you can use this:

case "$(file --dereference --mime $arg)" in
 *binary) echo "$arg is not text";;
 *) echo "$arg is text";;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.