Is there a commonly-installed Linux utility that will take a list of files and cat or gzip -d (==zcat) them based on whether the file is compressed?

An example of where I just needed this is when I want to examine /var/log/messages files, including both the logrotated ones (/var/log/messages-{date}.gz) and the current file.

I'll likely add something like the following to my ~/.zshrc:

catz () {
    local file
    for file ; do
        case $file in
            *gz*) gzip -d < $file ;;
            *bz*) bzip -d < $file ;;
            *xz*|*lz*) xz -d < $file ;;
            *) cat $file ;;

But, I'm not always on a machine where I have my rc installed, so I was hoping for a more general solution.


less does that.

less /var/log/messages*

If you pipe the output of less to another program, less will behave like cat or gunzip -c and not page the output.

  • Specifically, whatever program specified by $LESSPIPE does that. I tried that exact command earlier, was annoyed at having to ':n'/':p' through the list of files, and wrote the question. Coming back to this, I got what I'm actually going to use: less /var/log/messages* | less. Thanks.
    – benizi
    Nov 9 '11 at 22:53

The "more general solution" that you're looking for is to train your fingers to type out zcat -f … instead of trying to flip back and forth between zcat and cat.

By the way:

  • zgrep will search through both compressed and uncompressed files without an additional flag.
  • bzcat -f behaves like zcat -f - both will simply output (without decompressing) files that are not in their native compression format instead of understanding that it's a different compressed format and sub-forking off the right decompression program.

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.