Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to extract a tar archive to a specific directory. I've tried using -C flag but it doesn't work as expected. Here is the commandline I'm using

tar xvf myarchive.tar -C mydirectory/

This gives me a following error:

tar: file -C: not present in archive
tar: file mydirectory/: not present in archive

I've also tried setting the -C flag before the archive file but it just says this:

tar xvf -C mydirectory/ myarchive.tar
tar: -C: No such file or directory

What am I doing wrong?

EDIT: tar -tf shows that the tar archive does not have full path names:

tar -tf myarchive.tar
herareport/
herareport/bin/
...
share|improve this question
    
Which OS are you running? tar --version? –  quanta Aug 2 '12 at 13:57
    
IBM AIX. tar --version doesn't work –  palto Aug 2 '12 at 13:58
    
Please append the output of tar -tf myarchive.tar to your question. –  quanta Aug 2 '12 at 14:02
    
I did what you asked but can you specify why? The archive is very big so I can't put the whole output here. –  palto Aug 2 '12 at 14:09
    
Out of curiosity what's wrong with cd /new/directory ; tar xf /path/to/archive ? –  voretaq7 Aug 2 '12 at 15:02
show 5 more comments

4 Answers

up vote 3 down vote accepted

::dusts off Crusty Old Unix Monger badge::

From the comments:

If I try to use sudo -u myuser cd /home/myuser/; sudo -u myuser tar xvf /path/to/myarchive.tar it tries to extract to the wrong directory.

That's because sudo cd is not persistent (Try it yourself -- sudo cd / and then run pwd -- you'll be in the directory you started in, so your second sudo command isn't running where you want it).

To fix this you need to tell sudo to start a shell of its own so you can run multiple commands as the target user.
sudo -u myuser -s -- "(cd /home/myuser ; tar xvf /path/to/tarfile)" will probably do the trick.

(If for some reson your version of sudo dislikes this, sudo -u myuser sh -c "cd /home/myuser ; tar xvf /path/to/tarfile" should also work.)

(Of course none of this explains why -C isn't working as expected on AIX, but it should solve your immediate problem)

share|improve this answer
    
Technically you don't need my parentheses () above - I type them out of habit from years of tar cf - . | (cd /somewhere ; tar xvf -) directory cloning... –  voretaq7 Aug 2 '12 at 15:26
    
Thanks for helping me but it still doesn't work :) I tried sudo -u myuser -s -- "(cd /tmp/ ; pwd)" but it says /usr/bin/bash: (cd /tmp/ ; pwd): No such file or directory –  palto Aug 2 '12 at 15:27
    
Removing the parentheses just gives me the same error.. but without parentheses. –  palto Aug 2 '12 at 15:29
    
@palto Hmm... alternative added (invoke the shell manually with -c to specify the commands to run). –  voretaq7 Aug 2 '12 at 15:36
    
That -c did the trick, thanks. I'm marking this as the right answer. The -C option on the tar is broken on the AIX-machine so there might not be anything else that could be done. –  palto Aug 6 '12 at 6:20
add comment

Try

tar -xvC mydirectory/ -f myarchive.tgz 
share|improve this answer
    
tar -xvC mydirectory/ -f myarchive.tar tar: /dev/rmt0: No such file or directory –  palto Aug 2 '12 at 14:00
    
Well I can't seem to get the formatting right on these comments but I hope you get the picture what happens –  palto Aug 2 '12 at 14:01
    
Yeah, I just tested this on some old RS/6000 running in some dusty corner of our local DC and got the same message. –  SvW Aug 2 '12 at 14:04
add comment

Since your tarball was created with full path, download the GNU tar and try again to see if it works.

share|improve this answer
    
Can I use that without root privileges? I'm not actually a super user on this server :) –  palto Aug 2 '12 at 15:02
add comment

mv myarchive.tgz mydirectory/ && tar xvf mydirectory/myarchive.tgz

share|improve this answer
    
That still extracts to the parent directory, not mydirectory –  palto Aug 2 '12 at 14:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.