Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
@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
... so the -C did work. I stand corrected, money lost. :D – sebastian_k Sep 6 '11 at 15:14
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

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

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

Your Answer


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.