Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 ;;
        esac
    done
}

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

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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
add comment

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.