tar
stores relative paths by default. GNU tar even says so if you try to store an absolute path:
tar -cf foo.tar /home/foo
tar: Removing leading `/' from member names
If you need to extract a particular folder, have a look at what's in the tar file:
tar -tvf foo.tar
And note the exact filename. In the case of my foo.tar
file, I could extract /home/foo/bar
by saying:
tar -xvf foo.tar home/foo/bar # Note: no leading slash
So no, the way you posted isn't (necessarily) the correct way to do it. You have to leave out the leading slash. If you want to simulate absolute paths, do cd /
first and make sure you're the superuser. Also, this does the same:
tar -C / -xvf foo.tar home/foo/bar # -C is the ‘change directory’ option
There are very obvious, good reasons why tar
converts paths to relative ones. One is the ability to restore an archive in places other than its original source. The other is security. You could extract an archive, expect its files to appear in your current working directory, and instead overwrite system files (or your own work) elsewhere by mistake.
Note: if you use the -P
option, tar
will archive absolute paths. So it always pays to check the contents of big archives before extracting.
The command you're showing in your first line (tar -cvfz example2.tgz example1
) doesn't work and it should not output any file example2.tgz
. Didn't you get an error? Perhaps the file example2.tgz
existed already? Check if you have a file called z
in that folder - that's where the tgz has been saved to, because:
The -f
parameter specifies the file which must follow immediately afterwards: -f <file>
Try
tar cvzf exam.tgz example1
Best Answer
With GNU
tar
, you can do:With
bsdtar
:(
pigz
being the multi-threaded version ofgzip
).You could overwrite the file over itself like:
But that's quite risky, especially if the result ends up being less compressed than the original file (in which case, the second
pigz
may end up overwriting areas of the file which the first one has not read yet).