In this chapter, we’ll explore archiving and compression of folders and files in Linux. Archiving combines multiple files into a single file, while compression reduces the size of files to save disk space and speed up file transfers.

Summary

Linux file archiving and compression with tar, zip, zstd, and 7z on Debian 12 simplifies backups, transfers, and storage. Combining compression with gpg encryption ensures secure data management for beginners.

By the end, you’ll create archives, compress files, and extract them using:

tar – The universal archiver, gzip – Balance of speed and compression, bzip2 – Better compression ratios, xz – Highest compression efficiency, zip – Cross-platform compatibility, zstd – fast compression

Linux file archiving and compression enables efficient bundling, compression, encryption, and transfer of data. Learning these tools helps organize multiple files into a single archive for backups, reduces file sizes to optimize storage and speed up transfers, and secures archives with encryption for enhanced data protection.

Archiving and Compression Tools

1. tar (Tape Archive)

Purpose: Bundle files/directories into a .tar archive. Integrates with compressors via flags or pipelines.

2. gzip (GNU Zip)

3. bzip2

4. xz (LZMA)

5. zstd

6. zip / unzip

7. 7z (p7zip)

8. rar / unrar

9. brotli

10. gpg (Encryption)

Note: Brotli and gzip are both lossless compression algorithms widely used for web content and image optimization, but they differ in efficiency and performance. Brotli, developed by Google, typically achieves better compression ratios (smaller file sizes) than gzip, especially for text-based content like HTML, CSS, and JavaScript, making it ideal for reducing bandwidth usage and improving load times.

Gzip remains popular due to its faster compression/decompression speeds and broad compatibility esp. for images. Cloudflare and similar platforms enable automatic Brotli compression by detecting client support and serving Brotli-compressed content when possible, falling back to gzip if unsupported.


Combined & Advanced Workflows


# Backup a home directory, exclude caches, use gzip:
$ tar --exclude='*.cache/*' -czvf home-$(date +%F).tar.gz /home/alice

# Split a large tar.bz2 into 2 GB chunks:
$ tar -cjvf - bigdata/ | split --bytes=2G - bigdata.tar.bz2.part-

# Create a dual-compressed, encrypted archive:
$ tar -cvf - project/ \
  | zstd -19 -T0 \
  | gpg --encrypt --recipient bob@example.com \
  > project.tar.zst.gpg

Practical Exercises

  1. Create and extract a .tar.gz archive with exclusions.
  2. Compress a large log file with zstd at levels 3, 10, and 19; measure times.
  3. Build a password-protected .zip and verify with unzip -l.
  4. Encrypt a .tar.xz using symmetric and asymmetric gpg.
  5. Archive a directory with 7z, test integrity, then extract a single file.

Consolidated Summary Table

Tool Compress Extract Ext Notes
tar tar -cvf f.tar dir/ tar -xvf f.tar .tar bundling only
gzip / pigz gzip -1..-9 f
pigz -p4 f
gunzip f.gz .gz fast, multi-core
bzip2 / pbzip2 bzip2 -1..-9 f
pbzip2 -p4 f
bunzip2 f.bz2 .bz2 better ratio
xz xz -0..-9 -T4 f unxz f.xz .xz highest ratio
zstd zstd -1..-22 -T8 f unzstd f.zst .zst modern balance
zip zip -r [-e] f.zip dir/ unzip f.zip .zip cross-platform
7z 7z a [-p] [-mhe] f.7z dir/ 7z x f.7z .7z high ratio, encrypt
rar rar a f.rar dir/ unrar x f.rar .rar Windows-friendly
brotli brotli --quality=11 f brotli -d f.br .br web optimized
gpg gpg -c / gpg --encrypt gpg -d .gpg encryption

File Archiving and Compression
File Archiving and Compression

Best Practices

Archiving Tools

Archiving tools have been essential in Unix/Linux systems since the early days of computing. The first archiving tool, tar (Tape Archive), was introduced in the 1970s to store files on magnetic tapes. Over time, new tools emerged to address specific needs, such as compression, encryption, and file system imaging.

Beginner-Friendly Tools

  1. tar (Tape Archive) – a command-line tool for creating and extracting archives. tar is one of the oldest archiving tools in Unix/Linux.
  2. zip – a compression and archiving tool that creates .zip files. zip is widely used for compressing files and directories.
  3. gzip – a compression tool that reduces the size of files. gzip is often used in conjunction with tar to create compressed archives.

Moderate Complexity Tools

  1. bzip2 – a high-compression tool that provides better compression ratios than gzip. bzip2 is often used for compressing large files and archives.
  2. xz – a high-compression tool that provides better compression ratios than bzip2. xz is often used for compressing large files and archives.
  3. 7z – a compression and archiving tool that creates .7z files. 7z is widely used for compressing files and directories.

Advanced Tools

  1. cpio – a tool that creates and extracts archives, similar to tar. cpio is often used for creating archives of entire file systems.
  2. pax – a tool that creates and extracts archives, similar to tar and cpio. pax is often used for creating archives of entire file systems.
  3. dar – a disk archiving tool that creates and extracts archives of entire file systems. dar is often used for creating backups of entire systems.
  4. afio – a tool that creates and extracts archives, similar to tar and cpio. afio is often used for creating archives of entire file systems.
  5. star – a tool that creates and extracts archives, similar to tar and cpio. star is often used for creating archives of entire file systems.

Note: This is not an exhaustive list, and there are many other archiving tools available in Unix/Linux systems.


 File Archiving and Compression
File Archiving and Compression

Conclusion

File archiving and compression are essential skills for managing files efficiently in Linux. Whether you’re creating backups, saving disk space, or sharing files, tools like tar, gzip, bzip2, xz, zip, and zstdmake these tasks easy and effective. By mastering these tools, you’ll be well-equipped to handle file management like a pro.

Previous: Chapter 20 | Next: Chapter 22