Decompress a bzip file to a different location

The bzip2 command line tool decompresses .bz compressed files. By default it will delete the original file, naming it the same as the original file but without the extension and keep it in the same directory as the original file. This post looks at how to write it out to a different directory and/or with a different filename.

The defaults

The decompressed file will be named the same as the original file but without the extension as per the following rules from the bzip2 man page:

filename.bz2 becomes filename

filename.bz becomes filename

filename.tbz2 becomes filename.tar

filename.tbz becomes filename.tar

anyothername becomes anyothername.out

If the file does not end in one of the recognised endings, .bz2, .bz, .tbz2 or .tbz, bzip2 complains that it cannot guess the name of the original file, and uses the original name with .out appended.

The original file is deleted, and the decompressed file will always end up in the same directory as the original even if you are currently in a different directory.

For example, to decompress the file at /home/chris/test.txt.bz2 using the defaults you would do this:

bzip2 -d /home/chris/test.txt.bz2

The -d flag decompresses the file specified. The decompressed file would be at /home/chris/test.txt and the .bz version would no longer exist.

Saving the decompressed file with a different name

There is a flag to write the decompressed file out to standard output, and this is the only way I have worked out to write the decompressed data out to a different file. The -c flag writes the bzip2 decompressed data to standard output, leaves the original file intact and you can then redirect stdout into a file.

Using the same example as above, but this time saving the decompressed file to /tmp/test.txt you would do this: