◆Home Improvement Ideas Journal

Mastering macOS: Zip Files via Command Line - Examples

By

The macOS terminal is a powerful tool that allows users to interact with their Mac via commands. One of the most common tasks in the macOS terminal is compressing and decompressing files and folders into a .zip format. In this article, we will explore the built-in 'zip' command in macOS, offering examples for both compressing and decompressing files.

The 'zip' command in macOS provides a quick and efficient way to compress multiple files and folders into a single .zip file. It also supports the decompression of existing .zip files. Let's dive into the details of using these commands with step-by-step examples.

The 'zip' Command: Compressing Files and Folders

The basic syntax for using the 'zip' command to compress files is as follows:

How to use the zip Command
How to use the zip Command
Mac Command line and how to use it
Mac Command line and how to use it
an image of a computer screen with some text on it
an image of a computer screen with some text on it

zip [options] [- Ägypten destination.zip source destination files folders

Compressing a Single File

To zip a single file named 'example.txt' located in your home directory into a new .zip file named 'archive.zip', use the following command:

an image of a computer screen with the command window open and texting on it
an image of a computer screen with the command window open and texting on it
an image of a computer screen with text on it
an image of a computer screen with text on it
a black computer screen with some text on it
a black computer screen with some text on it

zip archive.zip /home/user/example.txt

Replace '/home/user' with the actual path to your home directory if it's different. This command will create a new .zip file named 'archive.zip' in your current directory, containing 'example.txt'.

Compressing Multiple Files and Folders

To zip multiple files and folders, simply list them all after the destination .zip file. For example, to zip 'file1.txt', 'file2.txt', 'folder1', and 'folder2', use the following command:

How to Use the Zip Command in Linux to Compress Files and Folders - Make Tech Easier
How to Use the Zip Command in Linux to Compress Files and Folders - Make Tech Easier
How to Use the Terminal Command-Line in macOS
How to Use the Terminal Command-Line in macOS
An A-Z Index of the Apple macOS command line
An A-Z Index of the Apple macOS command line
How to use the ping command on macOS
How to use the ping command on macOS
Command Line Basics Cheatsheet · Collin Berg
Command Line Basics Cheatsheet · Collin Berg
Mac Terminal  10 Awesome Commands
Mac Terminal  10 Awesome Commands

zip archive.zip file1.txt file2.txt folder1 folder2

This command will compress 'file1.txt', 'file2.txt', 'folder1' (including all its contents), and 'folder2' (including all its contents) into 'archive.zip'.

The 'unzip' Command: Decompressing Files and Folders

Unzipping a .zip file is as simple as using the 'unzip' command. Here are examples of how to decompress both individual files and entire directories.

an image of a black screen with text on it that reads,'how do you use the command? '
an image of a black screen with text on it that reads,'how do you use the command? '
Command Line Tutorial for Beginners: Mac, Windows & Linux
Command Line Tutorial for Beginners: Mac, Windows & Linux
an image of a computer screen with the text command line highlighted in black and white
an image of a computer screen with the text command line highlighted in black and white
Useful 7zip command line examples for terminal
Useful 7zip command line examples for terminal
Customize Your Mac Command Line With These Terminal Alternatives
Customize Your Mac Command Line With These Terminal Alternatives
a computer screen with the words linux commands on it and an image of a keyboard
a computer screen with the words linux commands on it and an image of a keyboard
an image of a computer screen with some text on it
an image of a computer screen with some text on it
Command Line Cheat Sheet
Command Line Cheat Sheet
5 Fun & Useful Terminal Commands You Can Use More Than Once [Mac & Linux]
5 Fun & Useful Terminal Commands You Can Use More Than Once [Mac & Linux]
How to Open Terminal on Mac | Mac Terminal Commands - MiniTool
How to Open Terminal on Mac | Mac Terminal Commands - MiniTool
Introduction To Linux Commands — Smashing Magazine
Introduction To Linux Commands — Smashing Magazine

Decompressing a Single File

To extract a single file named 'archive.zip' located in your home directory into the current directory, use the following command:

unzip archive.zip

This command will extract all files and folders from 'archive.zip' into the current directory. If there are subdirectories within the .zip file, they will be created accordingly.

Decompressing Specific Files from a .zip Archive

To extract only specific files from a .zip archive without extracting all of its contents, specify the file names after the .zip file name in the command. For example, to extract only 'file1.txt' from 'archive.zip', use:

unzip archive.zip file1.txt

This command will extract only 'file1.txt' from 'archive.zip' into the current directory.

Mastering the 'zip' and 'unzip' commands in the macOS terminal can significantly improve your productivity, especially when dealing with file compression and decompression tasks. Practice these examples and explore more of the 'zip' command's options to customize your compression process. Happy coding!