12

Say I have a file foo.tbz2 in a directory. I want to extract the tar file from the archive, but to a different directory. It seems like bunzip2 will only extract the archive to the same directory as the archive.

This works, but I'm wondering if there is a better way:

cd /another/directory
bunzip2 -k /original/directory/foo.tbz2
longneck
  • 410
  • 2
  • 4
  • 11

3 Answers3

18

You can use the other bzip2 tools:

bzcat foo.tbz2 > /another/directory/foo.tar

bzip2 -ckd foo.tbz2 > /another/directory/foo.tar

Note that if you want to untar it to another directory, you could use the -C option to tar:

tar xjf foo.tbz2 -C /another/directory
mrb
  • 10,048
  • 3
  • 36
  • 36
  • 1
    +1 for spreading the word on tar's `-C` flag. It's really handy, but hardly anyone knows it. – djf Aug 15 '12 at 21:59
  • 1
    I specifically do not want to extract the tar archive, but bzcat looks exactly like what I was looking for. Thanks! – longneck Aug 15 '12 at 22:54
0

Version from previous scripts (without double disk write - keep unpacked file in memory - in case of large files and slow disks it makes huge difference in speed):

#!/bin/sh

maxsize=2G
mkdir tmpfs 2>/dev/null
mount -t tmpfs -o size=${maxsize} tmpfs tmpfs

for i in $(ls -rS *.gz | sed 's/\.gz//');
do
     nice -n 19 gunzip -c ${i}.gz > tmpfs/${i}
     nice -n 19 bzip2 -c tmpfs/${i} > ${i}.bz2
     rm -f tmpfs/${i}
     if test -s "${i}.bz2"
     then
            rm -f "${i}.gz"
     fi
done
umount tmpfs
steve
  • 21,582
  • 5
  • 48
  • 75
Reigo
  • 1
0

I have suggestion: If you have a *bz2 file whether *.tar.bz2 or *.bz2 don't use bunzip just use tar command, Because tar command has many feature that can help you.

tar -jxf *.tar.bz2 -C yourplace/
or 
tar -jf *.bz2 -C yourplace/
PersianGulf
  • 10,728
  • 8
  • 51
  • 78
  • I don't want the tar file extracted. The end result of whatever command I use needs to be the tar file in a different directory than the bz2 file, and the original bz2 file must not be deleted or modified. – longneck Aug 16 '12 at 14:34
  • the above commands don't modify original files, note: tar file work each algorithm such as *gz *tar *lm *7 and so on. – PersianGulf Aug 16 '12 at 14:38