35

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?

4 Answers4

31

~ 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
symcbean
  • 5,540
  • 6
    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 08 '15 at 08:49
  • 1
    No - look at the original question - the last argument is an absolute path. – symcbean Oct 08 '15 at 10:43
  • 1
    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 08 '15 at 12:09
21

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

Example:

tar czf ~/files/wp/my-page-order.tar.gz \
  -C ~/webapps/zers/wp-content/plugins/ my-page-order
Lekensteyn
  • 20,830
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
Mattia72
  • 173
  • (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 06 '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 08 '19 at 07: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 09 '19 at 03:06
1

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.

AdminBee
  • 22,803
  • 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