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/
...
Best Answer
::dusts off
Crusty Old Unix Monger
badge::From the comments:
That's because
sudo cd
is not persistent (Try it yourself --sudo cd /
and then runpwd
-- you'll be in the directory you started in, so your secondsudo
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)