create tar with multiple directories and file locations

106

14

I have some scripts and files located in various locations like:

  • /etc/dir1
  • /var/www/html
  • /home/somedir

I want to make a tar file so that it copies files and folders with the location structure. When I untar to another location, all the files will be copied to their respective locations in the correct paths; same as where tar was made.

user1492502

Posted 2014-05-01T17:42:17.130

Reputation: 1 163

Answers

175

You can just use

tar -cf myfile.tar /etc/dir1 /var/www/html /home/somedir

also, you could use

tar -czf myfile.tar.gz /etc/dir1 /var/www/html /home/somedir

This second example (note the z in the -czf parameter) will compress the tar file using g(z)ip.

Jonathan

Posted 2014-05-01T17:42:17.130

Reputation: 1 943

3switch c to x when you want to extract. – n00b – 2018-07-14T02:04:43.987

0

This works for me small change by adding "change to directory DIR (C)" argument

tar -zcvf myfile.tar -C /etc/dir1 /var/www/html /home/somedir 

rupalis

Posted 2014-05-01T17:42:17.130

Reputation: 11