How to Zip a Folder in Ubuntu

The Payload Compression Vector

When you need to transfer a massive directory containing fifty highly complex configuration files and source code documents off an Ubuntu server, transferring them individually is highly inefficient and risks data corruption. You must utilize a compression algorithm to mathematically consolidate the entire directory structure and its contents into a single, easily transportable `.zip` archive.

How to Zip a Folder

The Ubuntu operating system relies on the command-line `zip` utility. If it is not present on your server, you must install it before attempting the compression.

1. Open your terminal application or connect to the server via SSH.
2. Prerequisite Verification: Ensure the utility is installed by executing:

sudo apt install zip -y

3. Navigate to the Target: Use the `cd` command to move into the directory that contains the folder you wish to compress. (For example, if the folder is located at `/var/www/html/backup`, you should navigate to `/var/www/html/`).
4. Execute the Compression: You must use the `zip` command, followed by the `-r` flag (which stands for “recursive,” instructing the utility to drill down and grab everything inside the target folder, not just the top level), the desired name of the final zip file, and finally, the name of the target folder. Type the syntax exactly like this:

zip -r backup_archive.zip backup/

5. Press Enter.
6. Analyzing the Output: The terminal will rapidly scroll, printing out the name of every single file as it is mathematically compressed and appended to the new archive. It will also display a percentage indicating how much space was saved on that specific file (e.g., `(deflated 45%)`).
7. When the prompt returns, type `ls -l` to verify the new `backup_archive.zip` file has been successfully written to the disk.

Get the best tech tips delivered straight to your inbox.

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