Archiving Files
tar is the Tape Archiver, a utility that was created long ago for backing up data to tape drives. Despite its age, it remains one of the most widely used archiving tools in Unix-like systems today.
Key Characteristics
By default, tar does not compress data—it simply bundles multiple files and directories into a single archive file. Compression can be added as an optional step using various compression algorithms.
Basic Operations
The three fundamental operations you’ll perform with tar are:
- Create archives
- Extract archives
- List archive contents
Creating an Archive
To create a basic archive without compression:
Flags explained:
-c= create a new archive-v= verbose (show progress)-f= specify the filename
Listing Archive Contents
To view what’s inside an archive without extracting it:
The -t flag lists the contents of the archive.
Extracting an Archive
To extract files from an archive:
By default, this extracts to the current directory. To specify a different output location, use the -C option:
Adding Compression
While tar bundles files, you can combine it with compression algorithms to reduce file size. Three common options are available:
| Flag | Algorithm | Speed | Compression Ratio |
|---|---|---|---|
-z |
gzip | Fast | Good |
-j |
bzip2 | Medium | Better |
-J |
xz | Slow | Best |
Compression Examples
Using gzip (fastest):
Using bzip2 (balanced):
Using xz (best compression):
Compression Comparison
Here’s a real-world comparison of the different compression methods applied to the same directory:
| File | Size | Compression |
|---|---|---|
| archive.tar | 1.3 GB | None (baseline) |
| archive.tgz | 629 MB | gzip |
| archive.tar.bz2 | 600 MB | bzip2 |
| archive.tar.xz | 512 MB | xz |
As you can see, xz provides the best compression ratio (reducing the size to just 39% of the original), but it takes the longest to process. Choose your compression method based on your priorities: speed vs. file size.
Quick Reference
Conclusion
The tar command remains an essential tool for system administrators and users alike. Understanding the trade-offs between different compression algorithms allows you to make informed decisions based on your specific needs—whether you prioritize speed, storage space, or a balance of both.