How to Compress a Folder into a Tarball in Ubuntu

The Data Consolidation Vector

When you need to back up a complex web server directory containing 5,000 individual PHP files and nested image folders, attempting to transfer them individually over an SSH connection is disastrously inefficient. The sheer overhead of opening and closing 5,000 separate network connections will cause the transfer to take hours. You must first mathematically consolidate all 5,000 files into a single, highly compressed archive block before transmission.

How to Compress a Folder into a Tarball

The Linux ecosystem relies on the tar (tape archive) utility, combined with the gzip compression algorithm, to generate standard `.tar.gz` archive files.

1. Open your terminal application or connect to your server via SSH.
2. Navigate to the parent directory containing the folder you wish to compress (e.g., if you want to compress `/var/www/html`, use `cd /var/www/`).
3. Execute the following command structure carefully:

tar -czvf website_backup.tar.gz html/

4. Command Breakdown:
* tar: Invokes the archiving utility.
* -c: Create a new archive.
* -z: Push the archive through `gzip` compression to drastically reduce the file size.
* -v: Verbose mode. Prints the name of every single file to the screen as it is packed, allowing you to monitor progress.
* -f: File name flag. You must immediately follow this with the desired name of your new archive (`website_backup.tar.gz`).
* html/: The target folder you are compressing.

5. Press Enter. The terminal will output a massive wall of text as it compresses the files. When the prompt returns, type ls -l to verify the existence of your new, highly compressed `.tar.gz` file.

Get the best tech tips delivered straight to your inbox.

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