How to Use the Linux tar Command to Compress Files

The File Transfer Problem

You are a web developer working on a Linux server. You need to back up a website directory containing 5,000 individual HTML, CSS, and image files, and download them to your local computer. If you try to download them one by one using a standard FTP client, the process will take hours because the computer has to negotiate a separate network connection for every single tiny image file.

To transfer data efficiently, you must bundle those 5,000 files into a single, massive file before moving it. In the Windows world, you would right-click and create a .zip file. In the Linux world, the standard tool for bundling and compressing files is the tar command.

The name stands for “Tape Archive,” originating from the 1970s when sysadmins used magnetic tapes for backups. Today, it is the most ubiquitous archiving tool in Linux. Understanding how to use tar to create and extract “tarballs” (the affectionate name for a .tar.gz file) is a mandatory skill for any developer.

Creating a Compressed Archive

The syntax for the tar command can look intimidating because it relies heavily on “flags” (single letters that dictate the command’s behaviour). However, there is only one string of letters you need to memorize to create a compressed archive: -czvf.

Here is what those letters mean:

  • c (Create): Tells tar to create a brand new archive.
  • z (Zip/Gzip): Tells tar to compress the file using the gzip algorithm, making the final file much smaller.
  • v (Verbose): Tells tar to print the names of the files on the screen as it processes them, so you know it isn’t frozen.
  • f (File): Tells tar that the very next word you type will be the name of the new archive file.

If you have a folder called website_files and you want to compress it into a single file called backup.tar.gz, you would type:

tar -czvf backup.tar.gz website_files/

When you press Enter, Linux will rapidly scroll the names of all 5,000 files across your screen. When it finishes, you will be left with a single, highly compressed backup.tar.gz file that you can download in seconds.

Extracting a Compressed Archive

Conversely, if you download a tarball from the internet (for example, the source code for a new software package) and you need to unpack it, you use almost the exact same flags. You simply swap the “c” (Create) for an “x” (Extract).

The magic string for extracting is -xzvf.

  • x (Extract): Tells tar to unpack an existing archive.

To unpack the file you created earlier into your current directory, type:

tar -xzvf backup.tar.gz

Listing the Contents Without Extracting

Sometimes you download a massive tarball and you are not sure what is inside it. If you blindly extract it, it might dump 10,000 files directly onto your Desktop, creating a chaotic mess.

You can peek inside the archive without extracting anything by using the -t (list) flag.

tar -tzvf backup.tar.gz

This will safely print a list of every file contained within the archive directly to your terminal, allowing you to verify its contents before deciding to unpack it.

Conclusion

Stop trying to transfer thousands of uncompressed files across a network. By memorizing the -czvf and -xzvf flags of the Linux tar command, you can instantly bundle, compress, and extract massive directories, saving enormous amounts of time and bandwidth.

Related posts

  1. Essential Linux Terminal Keyboard Shortcuts to Work Faster
  2. How to Find Large Files and Directories in Linux
  3. How to Create and Extract ZIP Files in the Linux Terminal

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

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