24

I try to create an archive with tar using relative paths. I use the following command:

tar czf ~/files/wp/my-page-order.tar.gz -C ~/webapps/zers/wp-content/plugins/ ~/webapps/zers/wp-content/plugins/my-page-order

But the archived files still have absolute paths. How can I use tar with relative paths?

24

'~' is expanded by the shell. Don't use '~' with -C:

tar czf ~/files/wp/my-page-order.tar.gz \
      -C ~ \
       webapps/zers/wp-content/plugins/my-page-order

(tar will include webapps/zers/wp-content/plugins/my-page-order path) or

tar czf ~/files/wp/my-page-order.tar.gz \
      -C ~/webapps/zers/wp-content/plugins \
       my-page-order

(tar will include my-page-order path)

Or just cd first....

cd ~/webapps/zers/wp-content/plugins
tar czf ~/files/wp/my-page-order.tar.gz my-page-order
  • 2
    Explanation is wrong. It doesn't matter here that ~ is expanded by shell. What matters is that -C changes current working directory as explained in anwer by Lekensteyn. – Piotr Dobrogost Oct 8 '15 at 8:49
  • 1
    No - look at the original question - the last argument is an absolute path. – symcbean Oct 8 '15 at 10:43
  • So what? Both instances of ~ are expanded and the whole invocation is not working as expected only because the last argument in case of using -C should have been given as relative path but instead is given as absolute path. Using ~ here does not matter at all. – Piotr Dobrogost Oct 8 '15 at 12:09
11

-C new_cwd changes the current working directory to new_cwd. The following arguments are then evaluated relative to new_cwd.

tar czf ~/files/wp/my-page-order.tar.gz -C ~/webapps/zers/wp-content/plugins/ my-page-order

4

The non GNU solution if tar has no -z option, just to mention:

pushd ~/files/wp; tar cvf - my-page-order | gzip > my-page-order.tar.gz && rm -rf my-page-order; popd

EDIT (with && and without rm):

pushd ~/files/wp && tar cvf - my-page-order | gzip > my-page-order.tar.gz && popd
  • (1) The question doesn’t say anything about rm; why do you include it in your answer? (2) Your answer archives the wrong directory. (3) Why have you changed tar -z to tar | gzip? (4) As long as you’re going to use &&, you should probably use && after the pushd, too. – G-Man Says 'Reinstate Monica' Aug 6 at 17:28
  • (1) You've right (2) really? Which is the good one? (3) this is the point: the non GNU version of tar (eg. on Unix), has no -z (4) I would use probably && in a script, but I think this is not necessary in a "one liner" – Mattia72 Aug 8 at 7:16
  • (2) OP wants to archive ~/webapps/zers/wp-content/plugins/my-page-order with output to ~/files/wp/my-page-order.tar.gz; your command archives ~/files/wp/my-page-order. (3) Thanks for editing your answer to explain why you used gzip. – G-Man Says 'Reinstate Monica' Aug 9 at 3:06

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.