Compress command output by piping to bzip2

Dom asked:

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

My 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

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.