tar with relative paths

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

~ 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

Method 2

-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

Method 3

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

Method 4

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x