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

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

  • 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


tar -xvC mydirectory/ -f myarchive.tgz 
  • 1
    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. – Sven Aug 2 '12 at 14:04

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

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

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

  • That still extracts to the parent directory, not mydirectory – palto Aug 2 '12 at 14:58

Extracts myarchive.tar to /mydirectory


cd /mydirectory
pax -rv -f myarchive.tar -s ',^/,,'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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