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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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