Is there a way to prevent or prompt you when trying to cat binary files?

Oftentimes I need to work with log files or data files, some of which are text and others not. It's easy to get mixed up, and accidentally trying to cat a binary file is annoying for obvious reasons.

Ideally I'd want an alias to cat that will prompt you if it detects a file is (with high probability) not plaintext.

  • its not ideal but using cat -e binary.file will convert all non printing characters and avoid your screen beeping. – rob Mar 17 '15 at 15:56

I really don't think this is worth it when you could just check before cating something, but you could set up a function in your ~/.bashrc to do this:

    for i in "$@"
        if  file --mime "$i" | grep "charset=binary" >/dev/null 
            printf "File %s is binary, skipping\n" "$i" >&2
            /bin/cat "$i"

That will run file to check if the file's mime-type is charset=binary and, if so, it will skip it. If that test fails, the file is not binary and /bin/cat is called. You can use it just like you would the regular cat:

$ cat /bin/bash /etc/os-release
File "/bin/bash" is binary, skipping
NAME="Linux Mint LMDE"

In addition to what said here you can simply use less instead of cat.

It prompts if the file is not a regular one, and give some plus as, for example, the list of the files in a zip,rar archive...

By the way you can use even cat -v that will transform non reading characters without adding a $ to the end of each line.


If you call file with -i it will return the mime type allowing you to determine if it is text or not.

file -i file.name | grep text/ && cat file.name

if file.name is binary the && does not execute the cat and if its plain text then it does.

This now matches script and other "text" files that are not marked as "plain"

  • That will fail on non-binary files that are not "text.plain". For example, shell scripts, python scripts, perl scripts etc. Not to mention html files and all the myriad text files that are not shown as "text-plain". – terdon Mar 17 '15 at 16:19

Save this as a script for example newcat.sh, and make it executable:

result=`file $1 | grep -c "binary"`
if [ "$result" -gt 0 ]
    echo "executable file"
        cat $1

then use it as ./newcat.sh filename

  • That will still print binary files that aren't LSB executables. Compressed archives and pdfs for example. – terdon Mar 17 '15 at 16:18
  • Corrected to include all binaries. – jcbermu Mar 17 '15 at 16:29
  • That's worse actually :) Still fails on pdfs and tarballs but now also fails on regular executable files (try /bin/ls for example) Better but that still fails on many (all?) binary formats. None of the files in my /bin directory are reported as 'binary'. Is anything? – terdon Mar 17 '15 at 16:32
  • @terdon My file reports "data" when it can't figure out the format. (Tried it on some garbage from /dev/urandom.) – a CVn Mar 18 '15 at 8:29
  • @MichaelKjörling yes, that's why I'm using the mimetype in my answer. That returns application/octet-stream; charset=binary for data from urandom. – terdon Mar 18 '15 at 11:47

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.