How to Use the ‘tar’ and ‘gzip’ Commands for File Archiving and Compression

The Difference Between Archiving and Compressing

In Windows, creating a .zip file performs two actions simultaneously: it gathers multiple files into a single folder (Archiving), and it shrinks the file size (Compressing).

In the UNIX philosophy, tools are designed to do exactly one thing perfectly. Therefore, Linux separates these two actions. You use the tar (Tape Archive) command to bundle 10,000 files into a single, massive file, and then you use the gzip command to shrink that massive file.

Because these tools are modular, they are incredibly robust and form the backbone of almost all automated Linux backup scripts.

1. Creating an Archive (tar)

Suppose you have a directory named /var/www/html/ containing 5,000 images and PHP scripts. You want to back up the entire website.

tar -cvf website_backup.tar /var/www/html/

The Required Flags:

  • c (Create): Tells the system to build a brand new archive.
  • v (Verbose): Prints the name of every single file to the screen as it is added, so you know the script hasn’t frozen.
  • f (File): Tells tar that the very next word (website_backup.tar) is the name you want to give the new file.

This command creates a single file. However, if the original folder was 50GB, the .tar file will also be exactly 50GB. No compression has occurred yet.

2. Adding Compression (gzip)

To shrink the file so you can transfer it across the network faster, you use gzip.

gzip website_backup.tar

This command deletes the original .tar file and replaces it with a highly compressed file named website_backup.tar.gz (often abbreviated as a “tarball” or .tgz).

3. The Ultimate One-Liner (Combining the Two)

Modern versions of tar are smart enough to call gzip automatically while they are building the archive, saving you a step and preventing massive temporary files from filling up your hard drive.

You simply add the z (Zip) flag to your initial command.

tar -czvf website_backup.tar.gz /var/www/html/

This single command bundles the 5,000 files and compresses them simultaneously, outputting the final, lightweight tarball.

4. Extracting an Archive

If a server crashes and you need to restore your backup, you must extract the tarball. You swap the c (Create) flag for the x (Extract) flag.

tar -xzvf website_backup.tar.gz -C /restore_location/

The -C flag is critical. If you don’t include it, tar will dump all 5,000 files directly into whatever folder you are currently standing in, which can cause a massive mess. The -C flag forces the extraction into a specific, safe directory.

Conclusion

Understanding the separation between tar and gzip is fundamental to Linux system administration. By combining these modular commands into a single, automated workflow, engineers can securely package, compress, and transfer massive directory structures without overwhelming local storage arrays.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.