Creating Archives with tar
Build .tar, .tar.gz, .tar.bz2, and .tar.xz archives and compare their sizes
tar is a two-step tool: it first archives (bundles files), then optionally compresses (reduces size). The flags z, j, and J invoke gzip, bzip2, and xz respectively. The c flag creates, v gives verbose output, and f specifies the filename.
Create a test workspace with some sample files to archive:
Create a raw tar archive (no compression) to establish a baseline size:
Note: the .tar file may be slightly larger than the source because tar adds header metadata per file.
Create a gzip-compressed archive (fastest, moderate compression):
Create a bzip2-compressed archive (slower, better compression):
Create an xz-compressed archive (slowest, best compression):
Compare all archive sizes side by side. The binary files (random data) resist compression — that is expected:
cCreate new archivexExtract archivetList contents (no extract)vVerbose (show filenames)fFilename follows this flagzUse gzip compressionjUse bzip2 compressionJUse xz compressionCChange to directory firstpPreserve permissionsListing and Extracting Archives
Inspect archive contents and extract to specific directories
List archive contents without extracting — essential before unpacking untrusted archives:
The t flag lists, f specifies the file. Notice paths include the source/ prefix.
Count how many files are in the archive:
31 entries = 1 directory + 20 binary files + 10 text files.
Extract the gzip archive into a specific target directory using -C:
Extract the xz archive with verbose output to see each file as it restores:
Extract only specific files from an archive (not the whole thing):
tar tf archive.tar.gz before extracting untrusted archives. Malicious archives can use path traversal (e.g., ../../etc/passwd) to overwrite system files. The list command lets you review paths before they land on disk.
Using gzip, bzip2, and xz Standalone
Compress and decompress individual files without tar
gzip, bzip2, and xz compress single files only — they do not bundle multiple files into one archive. That is tar's job. By default these tools replace the original file with the compressed version.
Create a test text file and compress it with gzip:
The text file compresses dramatically because it contains repeated content. The original test-gzip.txt is gone — replaced by .gz.
Decompress with gunzip (or gzip -d):
Use -k (keep) to compress without deleting the original:
Compress with bzip2 and compare the result:
bzip2 achieves better compression on text than gzip. Decompress with bunzip2 or bzip2 -d.
Compress with xz at maximum compression level and view stats:
The -9 flag uses maximum compression. Decompress with unxz or xz -d.
Compare all three tools on the same source file:
| Tool | Command | Extension | Speed | Ratio (text) |
|---|---|---|---|---|
| gzip | gzip / gunzip | .gz | Fast | Good |
| bzip2 | bzip2 / bunzip2 | .bz2 | Slower | Better |
| xz | xz / unxz | .xz | Slowest | Best |
Cross-Platform Archives with zip
Create and inspect .zip files compatible with Windows, macOS, and Linux
tar + gzip is the Linux standard, .zip archives are universally compatible with Windows and macOS without extra tools. Use zip when sharing files across operating systems.
Install zip if not present, then create a zip archive:
The -r flag is required for directories — without it, zip silently skips them.
List the zip archive contents (similar to tar tf):
Extract the zip archive to a specific directory:
Extract a single file from a zip archive without unpacking everything:
Compare final archive sizes across all formats:
Automated Backup Pipeline
Build a dated backup script using tar, gzip, and rotation logic
Create a backup destination directory and examine the dated filename pattern:
Run a one-liner backup of the source directory with a timestamp:
Simulate multiple backups accumulating over time (run a few times):
Write a rotation script that keeps only the 3 most recent backups:
Verify backup integrity by listing its contents before any restore attempt:
Lab Complete
You created archives in five formats, measured compression ratios, extracted to specific directories, and built an automated backup pipeline with rotation logic.