zip and unzip Basics: Cross-Platform Archives on Linux
What you'll be able to do
- Compress files and directories with `zip -r`
- Inspect an archive with `unzip -l` before extracting it
- Choose an extraction target and handle the overwrite prompt
Prerequisites (read these first)
What You'll Learn
Conclusion: Zip it up, unzip it open. Add content inspection and a fix for garbled filenames, and you're ready to swap archives with Windows users.
- Use
zipto compress files and directories into a .zip - Use
unzipto extract a .zip (including ones made on Windows) - Inspect archive contents before extracting, safely
- Know where files land and when they get overwritten
Who this is for: readers comfortable with basic file commands who need to exchange files with Windows.
Words used here (sorted out first)
- Compress: make a file smaller.
- Extract / decompress: turn it back into the original files. The two words mean nearly the same thing here; this article says "extract".
- Archive: several files bundled into one, or the format that does it. A
.zipis one kind of archive. - Current directoryA container that organizes files. Same idea as a "folder" on Windows or macOS.: where you are right now. Check it with
pwd. It is the default extraction target. ~(tilde): shorthand for your home directory, the space that belongs to you. It means the same as/home/username.
Intro: A zip file from a Windows user
Conclusion: Windows uses ZIP by default. With zip / unzip on Linux, both sides can send compressed files to each other.
report.zip. Can I open it on Linux?.zip is exactly what Windows creates when you right-click and "compress." On Linux you open it with unzip and create one with zip.tar we learned before?tar (.tar.gz) is what Linux folks use among themselves. The strength of .zip is that Windows opens it natively. So remember: "talking to Windows? use zip."1. What are zip / unzip, and how do they differ from tar?
Conclusion: zip both bundles and compresses in one step. Because Windows supports it natively, zip is the safe choice when the other side runs Windows.
zip and tar.| Format | Best for | Note |
|---|---|---|
.zip |
Exchanging with Windows | Windows opens it natively |
.tar.gz |
Linux / MacA system-wide access control model enforced by fixed rules (Mandatory Access Control). to each other | Preserves permissions precisely |
When in doubt: use zip for Windows, and tar.gz for Linux / Mac. That single rule is enough to start.
zip does "bundle" and "compress" in a single command, while tar only bundles and pairs with gzip to compress. Don't worry about that at first, though.2. Install zip / unzip
Conclusion: Ubuntu may not ship zip / unzip by default. Install them once with
sudo apt install zip unzip.
unzip: command not found.$ sudo apt install zip unzip
zip and unzip together?zip (the maker) and unzip (the opener) are separate packages, so installing both keeps you covered. To check whether they're installed, use which.$ which zip unzip
/usr/bin/zip /usr/bin/unzip
Two lines means both are installed. Only one line means the missing one isn't installed yet.
3. Compress with zip
Conclusion: For files,
zip name.zip file; for directories, add-ras inzip -r name.zip dir.
# Create a practice directory and move into it $ mkdir -p ~/zip-practice $ cd ~/zip-practice # Create practice files $ echo "id,name" > data.csv $ echo "memo" > memo.txt
Every example in this article runs inside ~/zip-practice. That way nothing you already had gets caught up in it.
3-1. Bundle files together
$ zip report.zip data.csv memo.txt
adding: data.csv (deflated 62%) adding: memo.txt (deflated 20%)
deflated 62% mean?report.zip, is the file being created, and the rest are the files going inside.Where it lands, and what happens to an existing zip
report.zipis created in the directory you are in right now. Check it withpwd- If a zip of that name already exists,
zipadds to and updates it. It does not start over - To replace the contents, delete the old zip first or pick a different name
3-2. Compress a whole directory (-r is required)
$ zip -r project.zip project/
The key gotcha: when compressing a directory, -r (recursive — include everything inside) is required. Forget -r and you get a nearly empty zip with no contents.
zip backup.zip myfolder and then deleted the original folder. When I opened the zip later, nothing was inside.-r. Bundle directories with zip -r backup.zip myfolder/.unzip -l before you delete anything. With that check in place, you never find out too late.tar also treated directories specially.zip, -r plays that role. Make it a reflex: bundling a directory means adding -r.4. Extract with unzip
Conclusion:
unzip name.zipextracts into the current directory. Use-dto set a target so files don't scatter.
4-1. Extract into the current directory (basic)
$ unzip report.zip
Archive: report.zip inflating: data.csv inflating: memo.txt
inflating means "expanding," so it's extracting.deflate (shrink), extraction is inflate (expand). They're a pair.4-2. Set the target directory (-d prevents accidents)
$ unzip report.zip -d ~/work/report
Avoid clutter: with no target, unzip dumps files right where you are. For archives with many files, pointing -d at a dedicated directory keeps things tidy. If the directory doesn't exist, unzip creates it for you. If the zip contains a directory, that directory is created under the place you gave to -d.
4-3. When a file of the same name already exists
The overwrite question, up front
When unzip finds a file with the same name, it stops and asks.
replace data.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename:
yoverwrites this one;Aoverwrites everythingnkeeps this one;Nkeeps everything (overwrites nothing)rextracts under a different name- Overwritten content cannot be recovered. Nothing goes to a trash folder
- When unsure, answer
Nand re-extract into a separate directory with-d zipandunzipdo not run in this site's virtual terminalAn interactive program that reads the commands you type and runs them.. Run them in your own terminal- Practice only inside the
~/zip-practiceyou made in section 3. Never practice where your important files live
To skip the promptA symbol (like $ or #) shown when the shell is waiting for your input. entirely, pick one of these two. Learn the safe one first.
# Safe side: never overwrite; keep existing files $ unzip -n report.zip # Risky side: overwrite everything without asking $ unzip -o report.zip
-o asks nothing at all. Use -o without looking inside first and same-named files vanish silently. Always check the contents with unzip -l before using -o. When unsure, use -n or -d instead.
5. Inspect contents before extracting
Conclusion:
unzip -l name.ziplists contents without extracting. Always inspect an unfamiliar zip before opening it.
-l (list) and you can see just the file listing, without extracting.$ unzip -l report.zip
Archive: report.zip
Length Date Time Name
--------- ---------- ----- ----
1024 2026-06-05 10:00 data.csv
256 2026-06-05 10:00 memo.txt
--------- -------
1280 2 files
Length is the file size in bytes, and Name is the filename stored inside.
For safety: for a zip from someone you don't know, always check the contents with unzip -l before extracting. Watching for odd paths like /etc/... or unfamiliar files is a habit that prevents accidents.
6. When filenames come out garbled
Conclusion: If non-ASCII names from a Windows zip look garbled, on Ubuntu use
unzip -O CP932 name.zipto extract them correctly.
\203t\202@...!The unzip on Ubuntu and Debian has an -O option to specify the filename encoding.
$ unzip -O CP932 windows.zip
Why this fixes it: -O CP932 tells unzip "the filenames in this zip are written in CP932." unzip then converts them correctly to UTF-8, and the names display properly. It works for inspection too: unzip -O CP932 -l windows.zip.
-O CP932 fixed it!-O is a handy option in the Ubuntu / Debian build of unzip. Some systems don't have it, but if you deal with Windows it's well worth knowing.7. Working with password-protected zips
Conclusion:
zip -emakes an encrypted zip, and unzip prompts for the password on extraction. Classic zip encryption is weak — don't over-rely on it.
7-1. Compress with a password
$ zip -e secret.zip data.csv
Enter password: Verify password: adding: data.csv (deflated 62%)
7-2. Extract a password-protected zip
$ unzip secret.zip
You'll be prompted for the password; enter it to extract.
Don't over-rely on it: traditional zip encryption is not strong. For genuinely sensitive files, don't depend on a zip password alone — consider a dedicated encryption tool or a secure transfer channel. Also note that forgetting the password locks you out of the contents for good, so keep a copy somewhere safe.
Practice: get your hands moving
Conclusion: Run the full flow yourself — compress, then inspect, then extract to a chosen target — to make it stick.
Work inside the ~/zip-practice you made in section 3. Confirm where you are with pwd before starting.
$ pwd
/home/user/zip-practice
Exercise 1: compress a directory
Task: create a directory called mydata with some files inside, then compress it into mydata.zip.
Show Hint 1 (Direction)
Bundling a directory needs an instruction that means "include everything inside." Without it you get a nearly empty zip.
Show Hint 2 (Command name)
Prepare with mkdir and touch, then bundle with zip -r.
Show Answer
$ mkdir mydata $ touch mydata/a.txt mydata/b.txt $ zip -r mydata.zip mydata/
adding: mydata/ (stored 0%) adding: mydata/a.txt (stored 0%) adding: mydata/b.txt (stored 0%)
Two files on adding: lines means it worked. The order can vary by system.
Exercise 2: inspect, then extract
Task: list the contents of mydata.zip, then extract it into a directory called extract.
Show Hint 1 (Direction)
First look inside without opening it. Then send the files somewhere other than where you are.
Show Hint 2 (Command name)
Inspect with unzip -l, and set the target with unzip -d.
Show Answer
$ unzip -l mydata.zip $ unzip mydata.zip -d extract $ ls extract/mydata
a.txt b.txt
extract is created automatically if it doesn't exist. The files land under extract/mydata/, because — as adding: mydata/a.txt in Exercise 1 showed — the zip carries the mydata/ directory name inside it. -d decides where to unpack; the structure inside the zip is reproduced as-is.
Common mistakes and fixes
Conclusion: Four classics — forgetting -r, command not found, scattered extraction, and garbled names. Learn each cause with its fix.
Mistake 1: compressed a directory but it's empty
zip backup.zip myfolder but it has almost nothing inside.-r. Bundle directories with zip -r backup.zip myfolder/. Then look inside with unzip -l.Mistake 2: unzip: command not found
unzip and got command not found.sudo apt install unzip. To make zips you need the zip package too.Mistake 3: extraction cluttered my current directory
unzip spreads files where you stand unless you say otherwise. Next time use unzip xxx.zip -d target/. Running unzip -l first also helps.Mistake 4: garbled filenames
unzip -O CP932 xxx.zip usually fixes it. Windows stored those names in CP932.Three-Line Recap
Conclusion: Bundle with zip -r, peek with unzip -l, and extract safely with unzip -d.
- Bundle directories with
zip -r, then count the contents withunzip -l - Extraction spreads into the current directory by default. Choose the target with
unzip -d target/ - On a name clash
unzipasks. When unsure answerNand re-extract somewhere else
Copy-paste patterns
# Create (directories require -r) zip -r archive.zip dir/ # Inspect (check before extracting) unzip -l archive.zip # Open (no clutter) unzip archive.zip -d target/ # Garbled names (Ubuntu / Debian) unzip -O CP932 windows.zip