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

Posted on 2004-11-03
Last Modified: 2013-12-26
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
Question by:mkisiu
    LVL 12

    Expert Comment

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


    LVL 12

    Expert Comment

    Oops, should have been

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

    LVL 12

    Expert Comment

    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)
    LVL 48

    Expert Comment

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

    LVL 2

    Expert Comment

    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.
    LVL 3

    Expert Comment

    tar --directory=/your/directory -xvf your_file.tar

    where /your/directory is the directory in which you want to untar the files to....
    LVL 2

    Accepted Solution

    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)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now