Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tar - extract to alternate directory

Status
Not open for further replies.

hcclnoodles

IS-IT--Management
Jun 3, 2004
123
GB
Hi there, if i am in /opt and i want to extract /data/package.tar to /usr, how do I do it...How can I extract to a directoory that im not currently in ???

eg ....(this doesnt work)

#cd /opt
# tar xvf /data/package.tar /usr

cheers
 
I'm not sure I understand the requirement to stay in /opt but you could try something like
Code:
( cd /usr; tar -xvf /data/package; )
To see this in action try
Code:
( cd /usr; pwd; ); pwd

Ceci n'est pas une signature
Columb Healy
 
GNU tar has a -C option (a.k.a. --directory) which should do the same.

Annihilannic.
 
ok my tar man page says it supports | -C directory |

so

if I run

# cd /opt
# tar xvf /data/package.tar -C /usr
#

it still doesnt work, it just goes straight to the next prompt

Columb, The reason I need it to extract to different directory is because im actually issuing the command remotely and as such cannot cd into the directory (im using a single line ssh command like this

# ssh $hostname 'tar xvf /data/package.tar -C /usr'


 
There is nothing preventing you from doing:

[tt]ssh $hostname 'cd /usr ; tar xvf /data/package.tar'[/tt]

Annihilannic.
 
Hi,

If you haved "tared" your archive with absolute path, (/absolute/path/to/file), it will extract to that absolute path.
If you have tared you archive with relative path ( ./part/of/path/file), it may be extracted to any directory continuing with the part of path.

If you have absolute path and want to extract to another location, try pax.

sample: substiture directory1 by directory2
Code:
pax -r -f archive.tar -s/directory1/directory2/

man pax for more details


 
I don't know linux, but shouldn't

tar xvf /data/package.tar -C /usr

have been

Code:
tar -C /usr xvf /data/package.tar

Don't switches usually come before arguments?

Then again, it's probably the absolute path name issue that aau mentioned.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
I don't think it matters, Razalas. Unless I'm much mistaken, you can either supply or omit the "-" to no effect. Also it doesn't matter where the switches or their parameters appear in the command as long as they are in the right order, i.e. this should work:

[tt]tar xvfC /data/package.tar /usr[/tt]

Annihilannic.
 
When reading the man page for tar, I get the impression that -C flag only has "chdir" meaning for tar when creating an archive file. On extracting files, -C is treated as a restore-dir-file specification (at least that's how it behaves on AIX).

I'm with AAU on the pax route - pax should be able to read tar archives and even modify the absolute pathnames when restoring. For relative pathnames, you can chdir first and then restore like Annihilannic suggests.


HTH,

p5wizard
 
Why do you want to do this, why not?

cd /usr
tar xvf /data/package.tar

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
It's Friday.......

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top