Linux – Compress Command Output by Piping to bzip2

bashlinux

Is it possible to pipe a command output to bzip2 for compression to an output file?

Something along the lines of:

cat somefile.txt | bzip2 --output somefile.txt.bz2

Best Answer

You can do this with bzip2's -c option:

       -c --stdout
              Compress or decompress to standard output.

For example:

command | bzip2 -c > some.txt.bz2

And to decompress:

bzip2 -dc < some.txt.bz2 | less