Prevent cat from opening a binary file

2

0

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.

bobdole

Posted 2015-03-17T15:46:48.540

Reputation: 43

its not ideal but using cat -e binary.file will convert all non printing characters and avoid your screen beeping. – rob – 2015-03-17T15:56:41.590

Answers

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"

terdon

Posted 2015-03-17T15:46:48.540

Reputation: 45 216

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"

rob

Posted 2015-03-17T15:46:48.540

Reputation: 575

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 – 2015-03-17T16:19:43.090

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.

Hastur

Posted 2015-03-17T15:46:48.540

Reputation: 15 043

-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

jcbermu

Posted 2015-03-17T15:46:48.540

Reputation: 15 868

That will still print binary files that aren't LSB executables. Compressed archives and pdfs for example. – terdon – 2015-03-17T16:18:25.553

Corrected to include all binaries. – jcbermu – 2015-03-17T16:29:35.760

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 – 2015-03-17T16:32:57.487

@terdon My file reports "data" when it can't figure out the format. (Tried it on some garbage from /dev/urandom.) – a CVn – 2015-03-18T08:29:12.593

@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 – 2015-03-18T11:47:57.577