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?

1

4 Answers 4

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
3
  • 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. Commented Oct 8, 2015 at 8:49
  • 1
    No - look at the original question - the last argument is an absolute path.
    – symcbean
    Commented Oct 8, 2015 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. Commented Oct 8, 2015 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
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
3
  • (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. Commented Aug 6, 2019 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
    Commented Aug 8, 2019 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. Commented Aug 9, 2019 at 3:06
2

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.

1
  • 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
    Commented Feb 23, 2021 at 10:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .