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
mkisiuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stefan73Commented:
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
0
stefan73Commented:
Oops, should have been

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

0
stefan73Commented:
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)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

TintinCommented:
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).

0
Peter-DartonCommented:
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.
0
prady_21Commented:
tar --directory=/your/directory -xvf your_file.tar

where /your/directory is the directory in which you want to untar the files to....
0
Peter-DartonCommented:
Use of --directory has the same effect of just "cd"ing to that directory before you start.
If the tar file was created with absolute paths in the first place, it won't make any difference.

That said, versions of tar that support the --directory option tend to be the newer versions that also force paths to be stored as relative paths.

However, having just re-read the original question (doh!) I can see that the original tar file wouldn't have had any absolute paths in it anyway, so I guess the potted-answer to the original question would be

mkdir -p /your/directory
cat my_file.tar | ( cd /your/directory ; tar -xf - )

which is the same as

mkdir -p /your/directory
cat my_file.tar | ( tar --directory=/your/directory -xf - )

which has the same effect as

mkdir -p /your/directory
cd /your/directory
tar -xf ..../my_file.tar  (where the .... is replaced by the relative or absolute path to the tar file, as we won't be in the directory it was created from anymroe)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.