How to Compress a Folder in Ubuntu

The Cryptographic Compression Vector

When operating a headless Ubuntu server, attempting to transfer a web directory containing 14,000 individual `.php` and `.css` files via standard SCP or FTP is mathematically disastrous. The network protocol overhead required to initiate a transfer handshake for every single micro-file will cripple the transfer speed. You must instruct the Linux kernel to execute a mathematical compression algorithm, crushing the entire directory hierarchy and its raw binary data into a single, dense archive file prior to transmission.

How to Compress a Folder in Ubuntu

The Linux environment relies on the venerable `tar` (Tape Archive) utility, which seamlessly combines multiple files into a single continuous data block, and then applies the `gzip` algorithmic compression to shrink the total byte count.

1. Open your terminal application or connect to the server via SSH.
2. The Target Acquisition:
* Navigate to the directory residing directly above the folder you intend to compress.
* For example, if you want to compress `/var/www/html/backup`, you must navigate to the `/var/www/html/` directory.
* Type: cd /var/www/html/
3. The Execution Command (`tar`):
* The `tar` utility requires highly specific flags to execute a compressed archive creation.
* -c: Create a new archive.
* -z: Compress the archive using the `gzip` algorithm (crucial for size reduction).
* -v: Verbose mode (outputs every file processed to the terminal screen so you can verify the operation).
* -f: Specifies the exact filename of the new archive.
4. The Syntactical Construction:
* The syntax is: `tar -czvf [Name_Of_New_File.tar.gz] [Name_Of_Target_Folder]`
* Assume you want to compress the `backup` folder into an archive named `archive-2024.tar.gz`.
* Type exactly:
tar -czvf archive-2024.tar.gz backup/
* Press Enter.
5. The Algorithmic Processing:
* The terminal will erupt with a rapid list of every single file as the kernel mathematically injects it into the `.tar` block and subsequently crushes it via `gzip`.
* Once the terminal returns to a blank prompt, type `ls -lh` to view the newly generated, highly compressed `.tar.gz` file residing next to your original folder. It is now ready for rapid network transmission.

Get the best tech tips delivered straight to your inbox.

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