4

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/
...
palto
  • 209
  • 3
  • 12
  • Which OS are you running? `tar --version`? – quanta Aug 02 '12 at 13:57
  • IBM AIX. tar --version doesn't work – palto Aug 02 '12 at 13:58
  • Please append the output of `tar -tf myarchive.tar` to your question. – quanta Aug 02 '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 02 '12 at 14:09
  • Out of curiosity what's wrong with `cd /new/directory ; tar xf /path/to/archive` ? – voretaq7 Aug 02 '12 at 15:02
  • @voretaq7: he's asking why the `-C` option doesn't work (on AIX)? – quanta Aug 02 '12 at 15:05
  • @quanta `I'm trying to extract a tar archive to a specific directory.` <- doesn't require the (apparently broken?) `-C` option - unless you're right and somehow the archive was created with full paths (leading `/` preserved), but his `-t` output doesn't show a leading `/` – voretaq7 Aug 02 '12 at 15:07
  • Well basically what I'm doing is I have sudo rights to user "myuser". I want to create a command that will take a tar-file in my home directory and extract it to the home directory of myuser. How would I do that without the -C option? – palto Aug 02 '12 at 15:09
  • If I try to use `sudo -u myuser cd /home/myuser/ ; sudo -u myuser tar xvf /path/to/myarchive.tar` it still tries to extract to the wrong directory. – palto Aug 02 '12 at 15:12
  • @palto See my answer below (and if it works incorporate your previous comment into the question :-) – voretaq7 Aug 02 '12 at 15:21
  • As of September 2015, tar xf -C still doesn't work on AIX 6. I had to make a shell script which changes to the target directory, then does something like gunzip < filename.tgz | tar xf -. Since I run via single-command ssh it's okay to abandon myself in the new directory. – wilee Sep 03 '15 at 18:36

5 Answers5

3

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

voretaq7
  • 79,879
  • 17
  • 130
  • 214
  • 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 02 '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 02 '12 at 15:27
  • Removing the parentheses just gives me the same error.. but without parentheses. – palto Aug 02 '12 at 15:29
  • @palto Hmm... alternative added (invoke the shell manually with `-c` to specify the commands to run). – voretaq7 Aug 02 '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 06 '12 at 06:20
1

Try

tar -xvC mydirectory/ -f myarchive.tgz 
Sven
  • 98,649
  • 14
  • 180
  • 226
  • 1
    `tar -xvC mydirectory/ -f myarchive.tar` `tar: /dev/rmt0: No such file or directory` – palto Aug 02 '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 02 '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 02 '12 at 14:04
0

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

quanta
  • 51,413
  • 19
  • 159
  • 217
  • Can I use that without root privileges? I'm not actually a super user on this server :) – palto Aug 02 '12 at 15:02
0

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

Kenny Rasschaert
  • 9,045
  • 3
  • 42
  • 58
nowthatsamatt
  • 921
  • 1
  • 8
  • 11
0

Extracts myarchive.tar to /mydirectory

Commands:

cd /mydirectory
pax -rv -f myarchive.tar -s ',^/,,'
javaPlease42
  • 101
  • 2