How to decompress a .bz2 file

402

66

I have a file as: filename.bz2 I need to decompress.

I have tried the command: tar xvjf filename.tar.bz2, but it didn't work as the file is not a tar file.

How do I decompress this file?

Jury A

Posted 2012-09-28T19:40:38.043

Reputation: 4 165

Answers

528

Try the following:

bzip2 -d filename.bz2

Note, that this command will not preserve original archive file.

To preserve the original archive, add the -k option:

bzip2 -dk filename.bz2

Lewis Diamond

Posted 2012-09-28T19:40:38.043

Reputation: 5 414

6You can also use bunzip2, which defaults to using the -d (uncompress) option. – RonaldB – 2017-01-19T16:18:45.770

2@LewisDiamond I ran bzip2 -d vim-8.0.tar.bz2, that results vim-8.0.tar. I couldn't enter this. – alhelal – 2017-11-17T00:35:04.047

5@alhelal that's because it's a .tar.bz2 file. You unzipped the tarball, you're left with the unzipped tarbal. Extract it with tar -x vim-8.0.tar. Originally you could have used tar -xjvf vim-8.0.tar.bz2. – Lewis Diamond – 2017-11-17T15:52:36.343

1A slight correction to Lewis's comment, tar also needs -f (at least on Raspbian Wheezy) as follows tar -xf vim-8.0.tar – JulianHarty – 2017-12-17T14:29:48.140

67

To explain a bit further, a single file can be compressed with bzip2 thus:

bzip2 myfile.txt

tar is only required when compressing multiple files:

tar cvjf myfile.tar.bz *.txt

Hence, when uncompressing a .bz2 file use bunzip, when uncompressing a tar.bz2 file use tar xjvf.

Benj

Posted 2012-09-28T19:40:38.043

Reputation: 863

4You can just use tar xjf filename.tar.bz2. The v just adds verbose output. Keep your terminal clean! I also had problems running tar -xjf, so be sure to try running it sans the - – MrOodles – 2015-01-15T15:14:21.523

1tar xf should be sufficient with the BSD variant — the j flag is only used when compressing – Mark Fox – 2015-10-30T00:16:15.420

6Excellent advice about the xjvf, just saved me. Thanks! – Edgar Aroutiounian – 2013-08-17T03:22:03.240

20

Use the bunzip2 (or bzip2 -d) command to decompress the file. For more information see this man page,

Levon

Posted 2012-09-28T19:40:38.043

Reputation: 740

Link is broken Levon – ankii – 2019-05-19T06:31:30.160

14

bzip2 is mono-threaded, which means it will take a long time to decompress a large file.

To decompress a .bz2 file multithreadedly, you can use the free, open source program lbzip2:

sudo apt-get install lbzip2
lbzip2 -d my_file.bz2

-d indicates you wish to decompress the file. It would automatically determine how many threads it will use. To specify the exact number of threads you want to use, use the -n parameter, e.g.:

lbzip2 -d -n 32 my_file.bz2

A few more useful commands with lbzip2:

To compress a folder:

tar -c -I lbzip2 -f file.tar.bz2 folder_name

To uncompress a folder:

 tar -I lbzip2 -xvf file.tar.bz2

Parameters:

-I, --use-compress-program PROG
      filter through PROG (must accept -d)
-x, --extract, --get
      extract files from an archive
-v, --verbose
      verbosely list files processed
-f, --file ARCHIVE
      use archive file or device ARCHIVE

Some alternatives to decompress a .bz2 file multithreadedly:

pbzip2:

sudo apt-get install pbzip2
pbzip2 -d my_file.bz2

mpibzip2: designed to be used on on cluster machines.


If you need some large .bz2 files to experiment with: https://dumps.wikimedia.org/mirrors.html

For example (a 14 GB .bz2 file, 200 GB uncompressed):

wget http://dumps.wikimedia.your.org/wikidatawiki/20170120/wikidatawiki-20170120-pages-articles-multistream.xml.bz2 
lbzip2 -d -n 32 wikidatawiki-20170120-pages-articles-multistream.xml.bz2 

http://vbtechsupport.com/1614/ did the benchmark:

enter image description here


For further information regarding the parameters for lbzip2: http://explainshell.com/explain?cmd=lbzip2+-d+-n+32+my_file.bz2 :

enter image description here

Franck Dernoncourt

Posted 2012-09-28T19:40:38.043

Reputation: 13 518

3

bzip2 -dc my_file.tar.bz2 | tar xvf -

worked for me on cygwin

Vishal

Posted 2012-09-28T19:40:38.043

Reputation: 39

1It would be helpful if you could add a few sentences to your answer to explain what it does. – fixer1234 – 2015-05-21T17:25:03.723

2@fixer1234 '-c' option copies the decompressed output to STDOUT which is then piped to tar utility and presented as filename using '-' so you can simplify it as: bzip2 -d my_file.tar.bz2 ; tar xvf my_file.tar – sactiw – 2016-01-07T19:39:36.483

1I believe even this should work bzip2 -dc my_file.tar.bz2 | tar xv i.e. no need to use -f option and corresponding '-' sign after it because tar can directly read the from STDOUT through the pipe operator. Also, feel free to drop -v option if you don't want to list files being processed. – sactiw – 2016-01-07T19:45:02.250

2This doesn't help, since the original poster already mentioned that it's not a tar archive. – icedwater – 2016-01-15T02:29:24.433

-5

  1. Go to https://cloudconvert.com.
  2. Upload the file.
  3. Convert it into a .tar file.
  4. Download it.
  5. Extract it from there, in your terminal.

Mr. de Santos

Posted 2012-09-28T19:40:38.043

Reputation: 13

7Isn't suitable for really large files! – narendranathjoshi – 2016-02-03T09:04:10.023

11We're superusers. We want a terminal-based solution. – noɥʇʎԀʎzɐɹƆ – 2016-06-07T16:41:40.513

1@Mr. de Santos ... and thereby giving up control over your data. You can not be serious. – dirdi – 2019-09-16T18:22:16.450