David Hancock

Jan 27

From Linux Journal letters, a nice way to use text in compressed files without needing to know they’re compressed, or what they’re compress with…

function do_cat()
{
    local CAT
    CAT=cat
    case "$1" in
        *.gz) CAT=zcat;;
        *.bz2) CAT=bzcat;;
    esac
    ${CAT} "$1"
}

function smart_cat()
{
    local i
    for i in "$@"; do
        do_cat "$i"
    done
}

Put these in your shell profile, and you can do stuff like:

smart_cat *log* | grep 'somethinginteresting'

and it won’t matter if they’re gzipped, bzip2ed, or plain text. Plugging in another compression tool is a matter of adding a branch in the case statement in do_cat.