In the Windows environment, compiling multiple files into a single compressed archive is almost exclusively done using the “.zip” format. However, in the Linux and Unix ecosystems, the undisputed standard is the tarball, instantly recognizable by the .tar.gz file extension.
The term “tar” stands for Tape Archive, a utility originally designed to write sequential data directly to magnetic tape backups. Today, Linux administrators use the tar command to bundle dozens, hundreds, or thousands of complex files—preserving their strict permissions and symlinks perfectly—and then compress that bundle into a single, highly efficient file.
Understanding the Architecture (.tar vs .tar.gz)
Unlike a ZIP file, which compresses each file individually as it bundles them, creating a .tar.gz is technically a two-step process handled simultaneously by the command:
- Archiving (.tar): The
tarutility glues all the requested files and folders together into one massive, uncompressed block of data. - Compressing (.gz): It then passes that massive block to the gzip compression algorithm, which squashes the entire block down to reduce the file size.
How to Compress Files (Create a tar.gz)
To create a compressed archive, you use the tar command followed by four specific flags: -c (create), -z (use gzip), -v (verbose), and -f (filename).
The Syntax:tar -czvf name_of_archive.tar.gz /path/to/directory_or_file
Practical Example:
If you have a folder named “WebsiteData” in your current directory and you want to back it up into a compressed file named “backup_2023.tar.gz”, you would type:
tar -czvf backup_2023.tar.gz WebsiteData/
Because you included the -v (verbose) flag, the terminal will print a rapid list of every single file as it is added to the archive, allowing you to monitor the progress. When it finishes, you can use the ls -l command to verify the new file exists and check its compressed size.
How to Extract Files (Unzip a tar.gz)
Extracting a tarball is just as straightforward. You use the exact same command structure, but replace the “create” flag (-c) with the “extract” flag (-x).
The Syntax:tar -xzvf name_of_archive.tar.gz
Practical Example:
To unpack the backup file you just created into your current directory, you would type:
tar -xzvf backup_2023.tar.gz
The files will be extracted instantly, recreating the original “WebsiteData” folder structure perfectly, complete with all subdirectories and original file permissions.
Extracting to a Specific Destination Directory
By default, tar extracts everything into your current working directory. If you downloaded a software package to your Downloads folder but want to extract it directly into the /opt/ directory, you must use the uppercase -C flag followed by the destination path.
tar -xzvf software_package.tar.gz -C /opt/
(Note: Extracting files to system directories like /opt/ usually requires prefixing the command with sudo).
How to View Contents Without Extracting
If you find an old .tar.gz file on your server and have no idea what is inside it, you should not extract it blindly. You can peek inside the archive and view a list of its contents using the -t (list) flag.
tar -tzvf unknown_archive.tar.gz
This command prints the entire directory tree of the archive to your screen without extracting a single byte of data to your hard drive.