extract archived .bz2 files inside a .tar archive with a single command

1

1

i need to decompress .bz2 files inside a .tar archive (two step decompression)

For example:

requested_files_1.tar

has multiple .bz2 files inside.

How can I extract them with a single command in linux. I have tried this but it does n't work.

tar -xvf requested_files_1.tar | bzip2 -d

FlyingMGET

Posted 2014-04-16T15:50:55.543

Reputation: 13

Answers

1

Your pipe doesn't work because tar isn't extracting the files to stdout, it's just listing them. bzip2 therefore tries to decompress the list of filenames as plain text, which of course, is not a compressed file's contents (i.e., it's using the plain text as the contents of the file to be extracted).

In order to use the output from tar xvf as a list of filenames for bzip2 to extract, you can do the following:

bzip2 -d $(tar xvf requested_files_1.tar)

The v option for tar here is required to list the extracted files for bzip2.

Achilleas

Posted 2014-04-16T15:50:55.543

Reputation: 333

This is the perfect and most efficient piece of code. It worked flawlessly. it was even better when I put a for loop inside the parenthesis and made it to run for all the tar files in that directory. thanks again.

bzip2 -d $(for i in *.tar;do tar xvf $i;done)

– FlyingMGET – 2014-04-16T19:02:02.610

0

Sounds like a duplicate of

https://unix.stackexchange.com/questions/4367/extracting-nested-zip-files

So your script would be something like this:

#!/bin/sh

for f in `ls *.tar` ; do
    dir="${f%.*}"
    mkdir "$dir"
    cd $dir
    tar -xf "../$f"
    bunzip2 *.bz2
    cd ..
done

Dude named Ben

Posted 2014-04-16T15:50:55.543

Reputation: 839