How to Zip a Folder in Linux

If you have a massive directory containing thousands of server logs, or a website backup containing three hundred image files, you cannot simply attach that entire folder to an email or transfer it over an SSH connection as raw data. You must compress the entire directory structure into a single, highly portable archive file.

In Windows, this is handled by simply right-clicking and selecting “Send to Compressed (zipped) folder.” In the Linux terminal, you have two primary options: the standard zip command (which plays nicely with Windows users), and the tar command (which is vastly more powerful and is the absolute gold standard for Linux system administration).

Method 1: The standard zip Command (For Cross-Platform Sharing)

If you are compressing a folder on a Linux server, but you intend to download that archive and email it to someone using a Windows PC or an Apple Mac, you should use the universally recognized .zip format.

  1. Open your terminal and navigate to the directory where your folder is located.
  2. You must use the zip command with the -r flag. (The -r stands for “recursive,” commanding the software to dive into the folder and grab every single sub-folder and file inside it. If you forget this flag, it will just zip an empty folder shell).

The syntax looks like this: zip -r [name_of_new_archive.zip] [name_of_folder_to_compress]

For example, if you want to compress a folder named website_backup into a file named archive.zip, you would type:

zip -r archive.zip website_backup

The terminal will rapidly print out a list of every single file as it compresses it. When it finishes, you will see a brand new archive.zip file sitting perfectly next to your original folder.

(Note: If the terminal throws a “command not found” error, it means the utility is not installed. Simply run sudo apt install zip on Ubuntu to install it).

Method 2: The tar Command (The Linux Gold Standard)

If you are compressing server data that is only ever going to be opened on another Linux machine, you should absolutely never use zip. You should use tar combined with gzip compression. This creates a .tar.gz file (affectionately known as a “tarball”).

Tarballs compress significantly smaller than standard zips, and more importantly, they mathematically preserve the strict Linux file permissions and ownership data of every single file inside the archive.

  1. Open your terminal.
  2. Type the tar command using the -czvf flag cluster.
  • -c: Create a new archive.
  • -z: Compress the archive using the Gzip algorithm.
  • -v: Verbose mode (print exactly what is happening to the screen).
  • -f: File mode (allows you to name the output file).

The syntax looks like this: tar -czvf [name_of_new_archive.tar.gz] [name_of_folder_to_compress]

For example, to compress the website_backup folder, you would type:

tar -czvf archive.tar.gz website_backup

How to Unzip the Folders

If you need to instantly reverse the process and extract the files back into a normal folder:

  • For a .zip file: Use the unzip command.
    unzip archive.zip
  • For a .tar.gz file: Use the tar command with the exact same flags, but swap the “c” (create) for an “x” (extract).
    tar -xzvf archive.tar.gz

Get the best tech tips delivered straight to your inbox.

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