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?


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

tar czf ~/files/wp/my-page-order.tar.gz \
      -C ~ \

(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 \

(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
  • 3
    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. 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. Oct 8 '15 at 12:09

-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

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. Aug 6 '19 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 '19 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. Aug 9 '19 at 3:06

I tried with wildcard * but it gave me absolute paths as well so I tried later like this:

tar -czf ~/clientkeys.tgz -C /etc/openvpn/client/ .

This archives (compresses) all the given files without absolute paths.

It appears that with the dot (.) works well. Don't know why exactly. I hope the answer helps.

  • welcome to U&L, I reformat your answer to fit conventions used here. Please note that all other 10 years old answer already answer question.
    – Archemar
    Feb 23 '21 at 10:45

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.