How Do You Use tar to Create a Single File That Is a Copy of a Subdirectory and All of Its Files and Subdirectories?

Problem scenario
You want to recursively copy subdirectories from on Linux machine to another.  How do you copy them into one file with the directory structure for the purpose of transferring it later?

On the source server go to the parent directory that you want to compress into one file.  Run this tar command:

tar cvfz target.tgz subdirsource/

Now target.tgz will have all the directories of  subdirsource.

# If you want to uncompress target.tgz, run this command from a directory where you can write to (e.g., have permissions to create subdirectories):

tar zxvf target.tgz

Leave a comment

Your email address will not be published. Required fields are marked *