A gzipped tarball is a tar archive compressed using gzip. We can use two methods to create such tarballs:
- The first method is as follows:
$ tar -czvvf archive.tar.gz [FILES]
Alternatively, this command can be used:
$ tar -cavvf archive.tar.gz [FILES]
The -z option specifies gzip compression and the -a option specifies that the compression format should be determined from the extension.
- The second method is as follows:
First, create a tarball:
$ tar -cvvf archive.tar [FILES]
Then, compress the tarball:
$ gzip archive.tar
If many files (a few hundred) are to be archived in a tarball and need to be compressed, we use the second method with a few changes. The problem with defining many files on the command line is that it can accept only a limited number of files as arguments. To solve this problem, we create a tar file by adding files one by one in a loop with the append option (-r), as follows:
FILE_LIST="file1 file2 file3 file4 file5" for f in $FILE_LIST; do tar -rvf archive.tar $f done gzip archive.tar
The following command will extract a gzipped tarball:
$ tar -xavvf archive.tar.gz -C extract_directory
In the preceding command, the -a option is used to detect the compression format.