19

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
Dom
  • 458
  • 1
  • 5
  • 15

2 Answers2

22

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
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
6

The bzip2 utility will compress stdin but won't write it to stdout if stdout is a terminal. You can though use standard output redirection techniques.

command | bzip2 >somefile.txt.bz2

and to read it the usual tools are available e.g.

bzless somefile.txt.bz2
user9517
  • 115,471
  • 20
  • 215
  • 297