Operating System - HP-UX
1819682 Members
3645 Online
109605 Solutions
New Discussion юеВ

tar extract to different directory

 
Mike_21
Frequent Advisor

tar extract to different directory

I can't seem to find in the man pages the option to extract a tar file to another directory. I have done it before on other platforms.

Thanks
10 REPLIES 10
Rita C Workman
Honored Contributor

Re: tar extract to different directory

Are you wanting...

tar xvf -C


Rita
Christopher McCray_1
Honored Contributor

Re: tar extract to different directory

I was under the impression that unless the absolute path is specified, you could extract it anywhere.

Chris
It wasn't me!!!!
harry d brown jr
Honored Contributor

Re: tar extract to different directory

Christopher, you are correct, if the tar was a relative backup, it can be restored anywhere by simply "cd'ing" to any directory you want to restore in.

If the backup was ABSOLUTE, meaning, when you do a "tar -tvf |more" and the output shows a "/" (slash) in the beginning of the path, then you are toast.

From the man pages of tar:

There is no way to restore an absolute path name to a relative position.



Live Free or Die
John Bolene
Honored Contributor

Re: tar extract to different directory

Correct, if you have tarred the absolute path, there is nothing you can do about it, such as

tar -cf /tmp/tar.ball /var

but if you did
cd /var
tar -cf /tmp/tar.ball filespec

you can put them anywhere that you have cd'd to
It is always a good day when you are launching rockets! http://tripolioklahoma.org, Mostly Missiles http://mostlymissiles.com
Mike_21
Frequent Advisor

Re: tar extract to different directory

I thought the -C option would work, however this is only for archiving different dir's! It doesn't seem to work for the extraction.
harry d brown jr
Honored Contributor

Re: tar extract to different directory

Looky what I found, although I haven't tested it:

1. How do I untar a file with absolute paths to a relative location?

a. Method 1 (user)
1. /usr/bin/pax -r -s ',^/,,' -f file.tar

b. Method 2 (root)
1. /usr/bin/cp /usr/sbin/static/tar /tmp
2. /usr/bin/dd if=file.tar | /usr/bin/chroot /tmp ./tar xf -



from :
http://shells.devunix.org/~argoth/iaoq/#I.A1

Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: tar extract to different directory

Hi Mike:

If the 'tar' archive was created with absolute paths, then you can't use 'tar' to extract to a different directory.

*However*, try this with 'pax':

# mkdir newdir
# cd olddir
# pax -rw olddir newdir

Regards!

...JRF...
harry d brown jr
Honored Contributor

Re: tar extract to different directory

I just tested this method, and it works:

/usr/bin/pax -r -s ',^/,,' -f file.tar

But first cd into the directory you want to restore to. If the archive was absolute: /tmp/somefilename, and you cd to /opt, it will restore "somefilename" to /opt/tmp/.
Live Free or Die
Volker Borowski
Honored Contributor

Re: tar extract to different directory

This is what I like about this forum.
Being in UNIX Busines for years, but never heard about pax. Something one should definately know about. Thanks James (this is clearly worth 10 points).

Until now, my workaround has always been to track the pathnames, and then move away one of the lower directories temporary, substituting this directory by a symlimk pointing to my intended target. What a mess sometimes.....

Volker
Santosh Nair_1
Honored Contributor

Re: tar extract to different directory

Another alternative is to use GNU's tar which automatically strips the leading / thus making all the filenames relative paths rather than absolute paths. So if you have a file /etc/hosts in the archive, GNU's tar will extract the file into etc/hosts, i.e. in a directory called etc in your present directory.

Hope this helps.

-Santosh
Life is what's happening while you're busy making other plans