Link to home
Start Free TrialLog in
Avatar of mkisiu
mkisiuFlag for Poland

asked on

I would like to untar my tar archive to the new location.

Hi Experts,
could you please help me to resolve little problem with TAR?
I would like to untar my archive created with 'tar cvf my_file.tar *' into different location that it was created. Is it possible to do this?
Please tell me how.

Best regards
mkisiu
Avatar of stefan73
stefan73
Flag of Germany image

Hi mkisiu,
Sure. Just cd to a different directory and do

tar xf {full path to .tar file}

You can also mis-use tar's "-" pseudo-filename to copy something:

(cd directory 1; tar -cf - )  | ( cd directory2; tar -xf -)

Cheers!

Stefan
Oops, should have been

(cd directory 1; tar -cf - * )  | ( cd directory2; tar -xf -)

In case you want to untar without changing the shell's current directory, use a sub-shell:

(assuming bash or ksh here)
export tarf=$PWD/{tar file} ; ( cd {other directory}; tar -xf $tarf)
Avatar of Tintin
Tintin

There are two factors involved in untarring to a different location:

1.  The version of tar used
2.  Whether the files were backed up with absolute or relative paths (although this is effected by point 1).

As Tintin says, the way the tar-file was created in the first place is important.

e.g. "tar cf mytarfile.tar /home/directory" will, on many versions of tar, create a tar file where all the file paths are absolute, hence it will always restore to /home/directory, no matter what the current directory is.
The last time I used tar on Linux, however, tar told me "surpressing leading /" and wrote the filenames in the tar file as "home/directory", hence to restore that to the original place I'd have to "cd /" first, whereas to restore it to somewhere else I could just cd to that somewhere else before un-tarring it.


If you do have a tar file whose files were stored with absolute paths, things are more complicated, but entirely achievable - you "just" need to change where your root is with the chroot command.

e.g. to restore the tar file that contains "/home/directory" to, for instance, /home/myhome/tmp/home/directory, you would do "cat myfile.tar | chroot /home/myhome/tmp tar -xf -"

Note: You will probably need to be root to run chroot.

Another method, if you're not root on the machine in question, is to copy the tar-file onto a Windows PC and use WinZip or similar - that understands tar files - then to zip the files up again and you'll then have a zip file with relative paths.  Of course, then you'll need to ensure your unix box has unzip, but it's not uncommon.
tar --directory=/your/directory -xvf your_file.tar

where /your/directory is the directory in which you want to untar the files to....
ASKER CERTIFIED SOLUTION
Avatar of Peter-Darton
Peter-Darton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial