I created a tarball on Ubuntu 14.04 with:
cd /tmp
tar -cfvz archive.tar.gz /folder
Now I want to extract a specific folder in the tarball (which inside the tarball lies in /tmp
) into a specific folder:
cd /tmp
tar -xfvz archive.tar.gz folder/in/archive -C /tmp/archive
The result should be a new folder in /tmp
called archive
. Is this correct? Especially the missing slash (relative path) for the folder to be extracted and the absolute path with the leading slash for the folder to create?
Best Answer
Tl;dr
Since you are in
/tmp
already, you can just discard the-C
option (since by defaulttar
will extract files in the current working directory) and just add--strip-components=2
:GNU
tar
by default stores relative paths.Whether an archive uses relative paths can be checked by running
tar -tf archive | head -n 1
, which will print the path of the first file in the archive; if that file's path is a relative path, all the files in the archive use relative paths:To extract a single file / folder from an archive that uses relative paths without its ancestors into a relative path you'll need two options:
-C
and--strip-components=N
: in the example below the archivebash-4.3.tar.gz
uses relative paths and contains a filebash-4.3/doc/bash.html
which is extracted into a relative pathpath
(-C
specifies the directory in which to extract the files,--strip-components=2
specifies that the parent and the parent of the parent of the extracted files should be ignored, so in this case onlybash.html
will be extracted into the target directory):So, back to your command, since you are in
/tmp
already, you can just discard the-C
option (since by defaulttar
will extract files in the current working directory) and just add--strip-components=2
: