Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2707
  • Last Modified:

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
1 Solution
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 -)


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)
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now