Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 17 down vote accepted

'~' 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
share|improve this answer
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
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

-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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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