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
1
  • I don't recommend using bzip2, it's slower and has worse compression than xz
    – anna328p
    Mar 3, 2019 at 0:31

2 Answers 2

21

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
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
1
  • 1
    Strangely the documentation is pretty good for bzip2.
    – user9517
    Mar 1, 2019 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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