Compress Files to a .zip Archive

Compressing files into a .zip archive in the Mac Terminal is a quick and efficient way to save space, organize files, or prepare them for sharing. The Terminal allows you to use the built-in zip command to compress individual files or entire directories into a single archive.

In this guide, we’ll walk you through the steps to create a .zip archive from your files and folders using the Terminal.

Basic Usage of the zip Command

The zip command is used to compress files and directories into a .zip archive. The basic syntax for creating a .zip archive is:

zip archive_name.zip file1 file2 file3

In this example, archive_name.zip is the name of the resulting compressed file, and file1, file2, and file3 are the files you want to compress.

Compressing a Single File

To compress a single file into a .zip archive, navigate to the directory where the file is located using the cd command. For example, if you would like to compress the file data.txt in your current folder, you would do the following.

Run the zip command to compress the file:

zip data.zip data.txt

This will create a new archive named data.zip containing the data.txt file in the same directory.

Compress Files to a .zip Archive

Compressing Multiple Files

To compress multiple files into a single .zip archive, specify all the file names after the zip command. For example, if you want to compress file1.txt, file2.txt, and file3.txt into a single archive called files.zip, use this command:

zip files.zip file1.txt file2.txt file3.txt

This will create a single .zip archive containing all three files.

For example, in the following, we compress the files email.csv, data.txt, and ruler.svg files into archive.zip.

Compress Multiple Files to a .zip Archive

Compressing an Entire Directory

If you want to compress an entire directory, use the -r (recursive) option. This tells zip to include all files and subdirectories inside the specified directory.

zip -r archive_name.zip directory_name

For example, to compress a folder called mydir into a .zip archive named mydir.zip, use this command:

zip -r mydir.zip mydir

This will create a mydir.zip archive containing all files and subfolders within the mydir directory.

Compressing an Entire Directory to a .zip Archive

Excluding Files from the Archive

You can exclude certain files from the archive by using the -x option. For example, to compress all files in a directory but exclude .svg files, use the following command:

zip -r archive_name.zip directory_name -x "*.svg"

This will create a zip file for the specified directory but exclude any files matching the pattern .svg.

Compress Files to a .zip Archive - Excluding Files from the Archive

Compressing Files with Password Protection

If you want to protect your zip file with a password, you can add the -e option to the command. For example:

zip -e archive_name.zip file1.txt file2.txt

After running the command, you’ll be prompted to enter and verify the password. The archive will be encrypted, and users will need the password to extract its contents.

Compress Files to a .zip Archive - with Password Protection