How Do You Copy All the Files in a Given Subdirectory and Place Them into a New .tar.gz File?

Problem scenario
You want to make copies of every file in a subdirectory and create a single .tar.gz file.  What do you do?

Possible Solution #1 (the files with the full path aka directory tree they are in)
Run this command: tar -czvf archive.tar.gz /path/to/source/files/subdirectory/

The above command will take a copy of all the files in /path/to/source/files/subdirectory/ and create a single file that is compressed with those copies.  It will copy the full path too.  That is, when you decompress the files in a given directory (e.g., tmp), it will look like this:

/tmp/path/to/source/file/subdirectory/

All the files will then be in subdirectory.

Possible Solution #2  (just the files and no directories)
If you do not want the underlying subdirectory tree, then change directories to be in the subdirectory with the files and run this command:

tar -czvf archive.tar.gz .

# The above command will create a file called archive.tar.gz with all the files in the current directory with no directory path structure.  This may be more desirable.

Leave a comment

Your email address will not be published.