2

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
0

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:

cat(){
    for i in "$@"
    do
        if  file --mime "$i" | grep "charset=binary" >/dev/null 
        then
            printf "File %s is binary, skipping\n" "$i" >&2
        else
            /bin/cat "$i"
        fi
    done
}

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
PRETTY_NAME="Linux Mint LMDE"
NAME="Linux Mint LMDE"
ID=linuxmint
ANSI_COLOR="1;31"
2

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.

2

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

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

#!/bin/bash
result=`file $1 | grep -c "binary"`
if [ "$result" -gt 0 ]
then
    echo "executable file"
else
        cat $1
fi

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.