How to Zip a Folder in Linux Using the Command Line

If you need to send a massive folder containing hundreds of documents or images to a colleague, you cannot simply attach the folder to an email. Email servers only accept individual files. To send the folder, you must compress it into a single, unified archive file.

While Linux heavily relies on .tar.gz files for internal software distribution, the rest of the computing world (specifically Windows and macOS users) relies almost exclusively on the standard .zip format. If you are sending files from a Linux machine to a Windows user, you must use the zip command to ensure they can easily open the file without downloading third-party extraction software.

Step 1: Verify the Zip Utility is Installed

Unlike tar, the zip utility is not always installed by default on every single Linux distribution. Before attempting to compress a folder, you should verify the tool is available.

  1. Open your terminal application.
  2. Type the following command and press Enter:

zip --version

If the terminal prints out a version number and copyright information, you are ready to proceed. If the terminal prints an error stating “command not found,” you must install the utility. On Debian or Ubuntu-based systems, run sudo apt install zip. On Red Hat or Fedora systems, run sudo dnf install zip.

Step 2: Zip a Complete Folder (The -r Flag)

To successfully zip a folder and all of its contents, you must use the recursive flag (-r). If you forget this flag, the command will only compress the empty outer shell of the directory, and all the files inside will be left behind.

  1. Use the cd command to navigate to the location of the folder you want to zip (e.g., cd ~/Documents).
  2. Type the following command syntax:

zip -r ArchiveName.zip FolderName/

Let’s break down exactly what this command means:

  • zip: The program you are calling.
  • -r: The recursive flag. This tells the program to dive deep into the folder and grab every single file and sub-folder it finds.
  • ArchiveName.zip: This is the name of the brand new file you are creating. You must type the .zip extension yourself, and you can name the file whatever you want.
  • FolderName/: This is the name of the existing folder you want to compress.

Example: If you have a folder named ProjectFiles and you want to compress it into a file called FinalSubmission.zip, you would type:

zip -r FinalSubmission.zip ProjectFiles/

When you press Enter, the terminal will print a list of every file as it is added to the archive, along with a percentage indicating how much disk space was saved by compressing that specific file.

Advanced Trick: Excluding Specific Files

Often, folders contain hidden system files or temporary cache files that you do not want to send to your colleague. The most notorious example is the invisible .DS_Store file that macOS creates inside folders, or temporary backup files ending in ~.

You can tell the zip command to compress the entire folder except for those specific junk files by adding the -x (exclude) flag at the very end of the command.

zip -r FinalSubmission.zip ProjectFiles/ -x "*.DS_Store"

The asterisk (*) acts as a wildcard, instructing the program to exclude any file that ends with .DS_Store, no matter where it is hiding inside the folder structure. The resulting zip file will be clean and professional.

Get the best tech tips delivered straight to your inbox.

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