Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am archiving a folder using following command:

tar jcf "home/username/forum/forum.tar.bz2" /var/www/forum/

Then I am extracting using :

tar jxf forum.tar.bz2 ./

It extracts correctly, but creates /home/username/forum/var/www/forum folder. What do I need to do in order to extract it into /home/username/forum folder?

Thank you

share|improve this question
1  
My money is on that you can't do this with just tar. I'd say if you have to do this often use the -C argument and a short bash script. –  sebastian_k Sep 2 '11 at 19:16
1  
@sebastian_k. I think you have lost your money:). dv3500ea solved it with just tar command and this is what I was looking for. –  Bakhtiyor Sep 5 '11 at 11:15
1  
... so the -C did work. I stand corrected, money lost. :D –  sebastian_k Sep 6 '11 at 15:14
add comment

3 Answers

up vote 4 down vote accepted

When creating the archive use -C /var/www/ to change the current directory to /var/www while creating the archive.

tar jcf home/username/forum/forum.tar.bz2 -C /var/www/ forum

This will cause the 'forum' folder to be in the top level of the archive and will therefore extract directly into the current folder when you run:

tar jxf forum.tar.bz2 ./
share|improve this answer
add comment

In addition to dv3500ea's answer, you can use the next command to extract tarballs containing var/www/forum/ to forum/:

tar xjf forum.tar.bz2 --strip-components=2

var/ and www/ are two path components, hence the 2 in --strip-components.

If you're not sure what's inside a tarball, use the t option:

tar tjf forum.tar.bz2

Pipe it through less so you an use arrow keys for scrolling.

A general note about the options used:

  • x - extracts a tarball, I always place it as first option since it's most logical to see what a command does
  • c - creates a tarball
  • t - lists the contents of a tarball
  • j - make tar uncompress bzip2 compressed tarballs. This is redundant with the .bz2 extension, so you may remove this option as well
  • f - this option expects a filename as argument, it's either the tarball to be extracted (tar xf file.tar.bz2) or created (tar cf file.tar.bz2)
share|improve this answer
add comment

The fast (and possibly wrong) solution would be to change your command to change directories before tarring. IE:

cd /var/www/forum; tar jcf "home/username/forum/forum.tar.bz2" .
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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