How do I extract a tarball, skipping some of the leading directories?
Solution 1
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 ./
Solution 2
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
)
Related videos on Youtube
Bakhtiyor
Updated on September 18, 2022Comments
-
Bakhtiyor over 1 year
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
-
Admin over 12 yearsMy 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.
-
Admin over 12 years@sebastian_k. I think you have lost your money:). dv3500ea solved it with just tar command and this is what I was looking for.
-
Admin over 12 years... so the -C did work. I stand corrected, money lost. :D
-