How to Compress Files and Folders Using the macOS Terminal

macOS includes a built-in Archive Utility that allows you to easily compress files by right-clicking on them in Finder and selecting “Compress.” While this graphical method is convenient, it is not always practical. If you are managing a remote Mac server via SSH, or if you need to script an automated daily backup of a massive project directory, you must know how to compress files into standard ZIP archives directly from the terminal. For this, macOS provides the native zip command-line utility.

How to Compress a Single File or Folder

The basic syntax for creating a zip archive is incredibly straightforward. You specify the zip command, followed by the name you want to give the new archive, followed by the target file or folder you want to compress.

To compress a single text document, open your terminal and type:

zip backup.zip report.txt

The terminal will output the compression ratio (e.g., deflated 45%), and a new file named backup.zip will appear in your current directory.

Compressing a Directory (The Recursive Flag)

If you attempt to run the basic command on a folder (e.g., zip archive.zip my_folder), macOS will only compress the empty folder shell, completely ignoring all the files and subdirectories inside it. To properly archive a folder and all of its contents, you must use the -r (recursive) flag.

zip -r project_backup.zip /Users/admin/Documents/ProjectX/

This command will dive into the ProjectX folder, compress every file, traverse into every subfolder, compress those files, and bundle them all into a single, highly portable project_backup.zip archive.

How to Exclude Specific Files (Like .DS_Store)

One of the most frustrating aspects of zipping folders on a Mac is that macOS automatically includes hidden system files, such as .DS_Store. If you send this ZIP to a Windows user, they will see these useless “junk” files cluttering the archive. To prevent this, you can use the -x (exclude) flag to strip out unwanted files during the compression process.

zip -r clean_archive.zip my_folder -x "*.DS_Store"

Make sure you wrap the exclusion pattern in quotes. This command tells the utility to recursively compress my_folder, but immediately drop any file matching the .DS_Store name, resulting in a perfectly clean archive ready for cross-platform sharing.

Get the best tech tips delivered straight to your inbox.

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