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.


2 Answers 2


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.

  • 9
    switch c to x when you want to extract.
    – n00b
    Jul 14, 2018 at 2:04

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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