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:

tar -cvf my_archive.tar /home

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:

tar -tvf my_archive.tar

The -t flag lists the contents of the archive.

Extracting an Archive

To extract files from an archive:

tar -xvf my_archive.tar

By default, this extracts to the current directory. To specify a different output location, use the -C option:

tar -xvf my_archive.tar -C /path/to/destination

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):

tar -czvf /tmp/archive.tgz /home/antonis/.config

Using bzip2 (balanced):

tar -cjvf /tmp/archive.tar.bz2 /home/antonis/.config

Using xz (best compression):

tar -cJvf /tmp/archive.tar.xz /home/antonis/.config

Compression Comparison

Here’s a real-world comparison of the different compression methods applied to the same directory:

ls -l /tmp/archive*
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

# Create uncompressed archive
tar -cvf archive.tar /path/to/dir

# Create compressed archives
tar -czvf archive.tar.gz /path/to/dir   # gzip
tar -cjvf archive.tar.bz2 /path/to/dir  # bzip2
tar -cJvf archive.tar.xz /path/to/dir   # xz

# List contents
tar -tvf archive.tar

# Extract to current directory
tar -xvf archive.tar

# Extract to specific directory
tar -xvf archive.tar -C /destination/path

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.

my DevOps Odyssey

“Σα βγεις στον πηγαιμό για την Ιθάκη, να εύχεσαι να ‘ναι μακρύς ο δρόμος, γεμάτος περιπέτειες, γεμάτος γνώσεις.” - Kavafis’ Ithaka.