zip and unzip Basics: Cross-Platform Archives on Linux

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 zip to compress files and directories into a .zip
  • Use unzip to 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 .zip is 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.

Lina: Linny-senpai, a Windows user sent me a file called report.zip. Can I open it on Linux?
Linny-senpai: Absolutely. That .zip is exactly what Windows creates when you right-click and "compress." On Linux you open it with unzip and create one with zip.
Lina: Wait, isn't that different from the tar we learned before?
Linny-senpai: Great question. 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.

Lina: I always hesitate between zip and tar.
Linny-senpai: Think of it roughly like this.
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.

Lina: So I pick based on the other person's OS.
Linny-senpai: Exactly. One more detail: 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.

Lina: I tried to use it and got unzip: command not found.
Linny-senpai: Classic. Ubuntu sometimes doesn't include it out of the box. Install it once and you're set.
$ sudo apt install zip unzip
Lina: I install both zip and unzip together?
Linny-senpai: Yes. 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 -r as in zip -r name.zip dir.

Linny-senpai: From here on we create and overwrite files. Let's set up a practice directory and some practice files first.
# 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%)
Lina: What does deflated 62% mean?
Linny-senpai: It means "shrunk by 62%." zip reports the compression ratio for you. The first name, 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.zip is created in the directory you are in right now. Check it with pwd
  • If a zip of that name already exists, zip adds 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.

Lina: Senpai, I made a backup with zip backup.zip myfolder and then deleted the original folder. When I opened the zip later, nothing was inside.
Linny-senpai: You left out -r. Bundle directories with zip -r backup.zip myfolder/.
Lina: But the command looked like it succeeded. It was empty the whole time?
Linny-senpai: It was. So after making a backup, count the contents with unzip -l before you delete anything. With that check in place, you never find out too late.
Lina: Make it, look inside, then delete. The order matters.
Lina: tar also treated directories specially.
Linny-senpai: Good memory! In zip, -r plays that role. Make it a reflex: bundling a directory means adding -r.

4. Extract with unzip

Conclusion: unzip name.zip extracts into the current directory. Use -d to 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
Lina: inflating means "expanding," so it's extracting.
Linny-senpai: Right. Compression is 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:
  • y overwrites this one; A overwrites everything
  • n keeps this one; N keeps everything (overwrites nothing)
  • r extracts under a different name
  • Overwritten content cannot be recovered. Nothing goes to a trash folder
  • When unsure, answer N and re-extract into a separate directory with -d
  • zip and unzip do 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-practice you 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.zip lists contents without extracting. Always inspect an unfamiliar zip before opening it.

Lina: I'm a bit nervous opening a zip I received without looking first.
Linny-senpai: Healthy instinct. Add -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.zip to extract them correctly.

Lina: I opened a zip from a Windows user and the filenames turned into gibberish like \203t\202@...!
Linny-senpai: That's the number-one snag in Windows interop. Windows sometimes stores non-ASCII filenames in a legacy encoding such as CP932 (a Shift-JIS variant), which clashes with Linux's UTF-8 and looks garbled.

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.

Lina: Just adding -O CP932 fixed it!
Linny-senpai: -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 -e makes 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.

Linny-senpai: Let's run today's flow end to end: "bundle it, peek inside, open it safely" — three steps.

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.

Lina: "Bundle, peek, open" — done! This flow feels safe.
Linny-senpai: Perfect. Make those three steps a habit and you'll stay calm with any zip that comes your way.

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

Lina: I ran zip backup.zip myfolder but it has almost nothing inside.
Linny-senpai: You forgot -r. Bundle directories with zip -r backup.zip myfolder/. Then look inside with unzip -l.

Mistake 2: unzip: command not found

Lina: I typed unzip and got command not found.
Linny-senpai: The package isn't installed yet. Run sudo apt install unzip. To make zips you need the zip package too.

Mistake 3: extraction cluttered my current directory

Lina: I opened a zip full of files and my current folder is now a mess.
Linny-senpai: 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

Lina: Filenames from a Windows zip come out garbled.
Linny-senpai: On Ubuntu / Debian, 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.

  1. Bundle directories with zip -r, then count the contents with unzip -l
  2. Extraction spreads into the current directory by default. Choose the target with unzip -d target/
  3. On a name clash unzip asks. When unsure answer N and 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

Next Reading

Share this article

Next steps