Use the zip and unzip commands (or built‑in tools) to compress, extract, list, and modify archives with a few short keystrokes.
Rule of thumb: zip = create or add files, unzip = extract; add -r for recursion, -d to delete, -l to list.
Basic Concepts
ZIP archives are single files that bundle many items while applying lossless compression. The format works on Windows, macOS, and Linux, so a command written for one system usually works on the others. Most operating systems also provide a graphical double‑click method, but the command line gives you precise control and can be scripted.
Two utilities cover almost every need:
zip– creates a new archive or adds files to an existing one.unzip– extracts files, lists contents, or tests integrity.
Both accept a series of options that start with a dash (-). Options can be combined (e.g., -r for recursion) or written separately (-r -9 for maximum compression).
Create an Archive
To bundle files into archive.zip:
- Open a terminal or command prompt.
- Navigate to the folder that holds the files you want to archive.
- Run
zip archive.zip file1 file2 …. Replacefile1etc. with the actual names.
Example – compress a project folder named MyApp and all its subfolders:
zip -r MyApp.zip MyApp
The -r flag tells zip to walk the directory tree recursively. Adding -9 selects the highest compression level, which is slower but yields the smallest file:
zip -r9 MyApp.zip MyApp
If you need a password‑protected archive, include -e. You will be prompted for the password twice:
zip -re secret.zip confidential.txt
Remember that the password is not stored in the archive; it is required each time you unzip the file.
Extract an Archive
To unpack archive.zip into the current directory, type:
unzip archive.zip
If you want the contents placed in a specific folder, use the -d option:
unzip archive.zip -d DestinationFolder
On Windows, the built‑in File Explorer can also extract by double‑clicking the ZIP file and dragging items out. The command line works the same way on macOS and Linux.
When extracting a password‑protected archive, add -P yourpassword (no space) to supply the password non‑interactively. Be cautious: the password becomes visible in the command history.
List, Test, and Verify
Before extracting, you may want to see what the archive contains. Use the -l (lowercase L) flag:
unzip -l archive.zip
This prints a table with file names, sizes, and timestamps. To check that the archive is not corrupted, run:
unzip -t archive.zip
The -t option tests each entry without writing any files to disk. If the test reports “No errors detected,” the archive is safe to extract.
Update, Delete, and Rename Files Inside
Adding new files to an existing archive does not require recreating the whole file. Use the same zip command with the archive name first:
zip archive.zip newfile.txt
The -u flag updates only those files that have changed since the last archive operation, saving time on large projects:
zip -u archive.zip *.html
To remove a file from the archive, use zip -d:
zip -d archive.zip oldfile.log
Renaming a file inside the archive is a two‑step process: delete the old entry and add the file under the new name. For example, to rename draft.txt to final.txt:
- Extract
draft.txt(if you don’t already have a copy). - Delete the old entry:
zip -d archive.zip draft.txt. - Add the renamed file:
zip archive.zip final.txt.
While this seems extra work, it preserves the archive’s integrity and avoids the need for a full rebuild.
Common Options and Shortcuts
These flags appear most often and are worth memorising:
-r– recurse into directories.-9– maximum compression (slowest).-0– store only, no compression (fastest).-e– encrypt with a password.-q– quiet mode; suppress most output.-v– verbose; show detailed processing.-l(unzip) – list contents.-t(unzip) – test integrity.-d(zip) – delete entries.-u– update changed files.
On Windows PowerShell, you can call the same commands if you have the Info‑ZIP utilities installed, or you can use the built‑in Compress-Archive and Expand-Archive cmdlets. The syntax is slightly different:
Compress-Archive -Path *.txt -DestinationPath docs.zip -CompressionLevel Optimal
Expand-Archive -LiteralPath docs.zip -DestinationPath ./extracted
These cmdlets map to the same concepts: -CompressionLevel Optimal mirrors -9, and -Force overwrites existing files.
Practical Worked Example
Suppose you have a folder Photos/2024 with subfolders for each month, and you want a compressed archive named 2024Photos.zip that you can email.
- Open a terminal and change to the parent directory:
cd ~/Pictures - Create the archive with maximum compression and include all subfolders:
The quotes protect the space‑free name;zip -r9 2024Photos.zip "2024"-r9tells zip to recurse and use level 9. - Verify the size and list the contents:
ls -lh 2024Photos.zip unzip -l 2024Photos.zip - Test the archive before sending:
If the test passes, attach the file to your email.unzip -t 2024Photos.zip
If the recipient needs a password, add -e during creation and share the password through a separate channel.
Safety and Best Practices
Always keep an original copy of the files you are compressing until you have verified the archive with unzip -t. Deleting the source before testing can lead to data loss if the archive is corrupted.
When using passwords, choose a strong phrase rather than a short word. Remember that ZIP encryption is relatively weak compared to modern standards; for highly sensitive data, consider a stronger tool like 7z or gpg.
On macOS, the built‑in Archive Utility creates ZIP files with the -X flag (preserving extended attributes). If you need those attributes on another system, use the -X option with the command‑line zip as well.
Finally, avoid extracting archives from untrusted sources directly into important directories. Extract to a temporary folder first, inspect the contents, then move the safe files where you need them.