How to Zip a File in Linux

While the standard archive format in the Linux ecosystem is usually the .tar.gz file, you will inevitably need to send logs or configuration files to a Windows or macOS user. Because those operating systems natively rely on the .zip format, it is often easier to simply compress your files into a standard ZIP archive directly from the Linux terminal.

To do this, you use the dedicated zip command. It is important to note that the zip utility is entirely separate from the unzip utility; installing one does not automatically install the other.

Step 1: Install the zip Utility

If you are running a minimal server environment, the zip command is likely missing.

If you type the command and the terminal throws an error stating bash: zip: command not found, you must install the package using your distribution’s package manager.

  • On Debian/Ubuntu: Type sudo apt install zip
  • On CentOS/RHEL: Type sudo yum install zip
  • On Arch Linux: Type sudo pacman -S zip

Method 2: Zip Multiple Loose Files Together

The syntax for the zip command is slightly counter-intuitive. You must declare the name of the brand new ZIP file you are trying to create before you list the files you actually want to put inside it.

  1. Open your terminal and navigate to the directory containing your files.
  2. Type the zip command, followed by the name of the archive you want to create, followed by the specific files you want to compress.

zip my_archive.zip file1.txt file2.txt image.jpg

When you press Enter, the terminal will print a readout confirming that it is actively compressing (“deflating”) each file. When it finishes, you will find my_archive.zip sitting safely in your current directory, containing the three files.

Method 3: Zip an Entire Directory (Folder)

If you have a folder named website_backup containing hundreds of HTML and CSS files, you cannot simply type zip backup.zip website_backup. The command will literally only compress the empty folder itself, completely ignoring all the files inside it.

To force the zip command to dive inside the folder and compress every single file and sub-folder it finds, you must use the -r (recursive) flag.

  1. Type the zip command.
  2. Add the -r flag.
  3. Type the name of the new archive, followed by the name of the target directory.

zip -r website_backup.zip website_backup/

The terminal will rapidly scroll through every single file inside the directory as it compresses the entire tree into a single package.

Advanced Tip: Use Wildcards (*) to Zip Specific File Types

If you have a messy directory containing 100 images and 100 text files, and you only want to compress the text files, you do not need to type 100 filenames manually. You can use the asterisk (*) wildcard character to filter them.

zip logs.zip *.txt

This command instructs Linux to scan the current directory, grab absolutely every file that ends in .txt, ignore everything else, and compress them into logs.zip.

Get the best tech tips delivered straight to your inbox.

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