zip and unzip from Zero: Pack, List, Test, and Extract Just One File
One zip file carries a whole project folder — but only if you pack it right. This guide starts from zero: zip -r, listing with unzip -l before you unpack, testing with unzip -t, updating one file, excluding node_modules, extracting a single file, deleting entries. Every step was run for real on Zip 3.0 / UnZip 6.00; the transcript is included.

The easiest way to send someone a project folder is to pack it into a single zip file: one file to send, and the other side unpacks it and has everything — no sending files one by one, no worrying that one got lost halfway. zip does two things: packing, putting a pile of files into one package, and compressing, making what is inside a little smaller. This guide starts from zero on zip and its partner unzip: packing, listing, testing, extracting just one file, leaving files out, and deleting entries from a package. Every step was run for real on Zip 3.0 and UnZip 6.00, with the output in the test record at the end; every line of output quoted here matches that record word for word.
First, what zip is. The manual page puts it plainly: zip is a compression and file packaging utility, analogous to a combination of the Unix commands tar and compress, and compatible with PKZIP. Packing and compressing are two different jobs: packing puts many files into one container, compressing squeezes what is inside. tar only packs and never compresses, gzip only compresses a single file, and zip does both in one pass — which is why it is everywhere on Windows and macOS. The other side receives a .zip and unpacks it with nothing extra installed.
The material for the test run is a small project called project: index.html, style.css, data/notes.txt, assets/repeat.txt, plus a node_modules/dep/index.js — a node_modules directory is there on purpose, for excluding it in section 5. repeat.txt is 500 identical lines of hello zip demo 277, 9500 bytes in total, there to show what compression actually does. First confirm the versions: zip -v prints This is Zip 3.0 (July 5th 2008), by Info-ZIP., and unzip -v prints UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.. The version numbers look old, but the zip format itself has barely changed in decades — a package made by an old version unpacks fine on a new system, and that boring stability is zip's greatest strength.
It is worth saying up front when to use zip and when not to. Use it when you need to send a pile of files to someone — especially someone who might be on Windows or macOS and might not know the command line. A .zip sent over, double-clicked, unpacks with nothing to install, and paths, filenames, and modification times all survive intact. Another good moment is archiving: halfway through a project, pack a dated snapshot and put it aside — it takes less space than copying the whole directory and is easier to find later. Do not use it when the files have already been compressed once, such as photos and videos — packing them is still useful for tidiness, but -0, store without compressing, is faster. And backing up a whole server directory belongs to tar plus gzip; section 12 covers how the two divide the work.
1. The first step of packing: zip -r, and forgetting -r leaves an empty shell
The command to pack a directory is zip -r release.zip project, where -r means recurse: put the directory and everything inside it, subdirectories and files alike, into the package. That -r is the first pitfall in this guide, and the easiest one to hit. In the test run we deliberately left it out first and ran zip release.zip project: the output is a single line, adding: project/ (stored 0%), and the exit code is still 0 — the command succeeded, but the package holds only an empty directory. Listing it with unzip -l shows just 1 file and 0 bytes; index.html and style.css never made it in. Worse, nothing in the whole process reports an error. If you do not open the listing and look, you will never know the package is empty.
Run the correct command with -r and the output changes at once: adding: project/style.css (stored 0%), adding: project/index.html (deflated 17%), adding: project/assets/repeat.txt (deflated 99%) — one line per file. Remember the lesson: packing a directory with zip always needs -r, and after packing, look at the listing with unzip -l and confirm the files are all there before sending it to anyone. Sending an empty package is more embarrassing than sending the wrong one — the other side unpacks it, finds nothing, and assumes the transfer broke.
2. Look at the listing first: unzip -l is the most important step before unpacking
When you receive a zip package, do not unpack it in a hurry — look at the listing first: unzip -l release.zip. In the test run the listing has 10 files and 9646 bytes in total, one line per file with its name, uncompressed size, and modification date. Three things in a listing are worth reading. First, are all the files there: index.html at 63 bytes, style.css at 20, data/notes.txt at 42, project/assets/repeat.txt at 9500 — none missing. Second, are the paths right: every path in the package starts with the relative prefix project/, so unpacking creates a project directory in the current directory instead of scattering files everywhere. Third, is there anything extra: node_modules/dep/index.js is in there too — a dependency directory you usually do not want to send, and section 5 shows how to leave it out.
Reading the listing has another benefit: it shows where the package was packed from. Whatever the paths in the listing look like, that is what unpacking will reproduce. If the paths are absolute, like /home/lee/project/index.html, unpacking can cause trouble — section 9 explains how zip quietly handles absolute paths. Build the habit: before unpacking, run unzip -l. Three seconds spent, half an hour of cleaning scattered files saved.
3. How much did it actually compress: the percentage after deflated
When zip packs, every file gets a percentage after it: deflated 99%, deflated 17%, deflated 2%, or stored 0%. Deflated means "this file was compressed", and the number is how much was squeezed out; stored means "kept as it is, not compressed". In the test run repeat.txt is deflated 99%: 500 lines of repeated text, compressed down to one percent of its original size. index.html is deflated 17%, notes.txt is deflated 2% — short files have little to squeeze. style.css is only 20 bytes, so zip simply stored it: for a file that small, the space compression would save is less than the bookkeeping compression itself needs.
The whole package shows it best in file sizes: project/assets/repeat.txt is 9500 bytes on its own, and the entire release.zip is just 1892 bytes. In other words, 9500 bytes of repeated text plus a few small files pack down to under two thousand bytes. You do not need to memorise compression ratios — remember the pattern instead: repeated text compresses the most, short files and already-compressed files, like photos and videos, barely move. Pack a directory full of photos into a zip and the package comes out about the same size as the original; that is not zip failing, it is the photos having already been compressed once.
4. Testing the goods: unzip -t checks before you unpack
Before sending a package, and before unpacking one you received, run unzip -t release.zip: it unpacks every file in memory, compares the checksum (CRC) against the one stored in the package, and confirms nothing is damaged. In the test run the output is OK after every file, and the last line is No errors detected in compressed data of release.zip.. That line is the certificate that the goods passed inspection.
Testing looks unnecessary on an ordinary day and saves you on the day it matters. A package travels across a network; the transfer can corrupt it, the download can stop halfway, the disk can miswrite — and a package can be quietly damaged. Unpack a damaged package directly and it fails halfway, leaving half good files and half bad ones, with no telling which half is which. Test first and a bad package is exposed at the testing step; send it again and you are done. Testing your own package before sending it is also a courtesy to the other side: they receive a tested package, and if it will not unpack they know the transfer is the problem, not your package.
5. Updating and excluding: change one file without repacking everything
One good habit of zip: when the package already exists and you run zip -r again, it does not rebuild the package from scratch — it only updates the files that changed. In the test run we appended a comment line to index.html and ran zip -r release.zip project/index.html; the output is updating: project/index.html (deflated 15%) — only that one file was updated, everything else untouched. Updating one file at a time saves real time when the package is large.
Excluding uses -x: zip -r clean.zip project -x "project/node_modules/*" means "whatever is under node_modules, do not put it in the package". In the test run clean.zip has 7 files and 9642 bytes — down from 10 files, minus the node_modules directory and the index.js inside it. Note the quotes around the pattern after -x: without them, the shell expands the wildcard in the current directory first, and zip receives something different from the pattern you meant to write. Those quotes work on the same principle as set -a in the environment-variables guide: the shell does one extra step before the command runs, and knowing what it does is how you get around it.
6. Decide before packing: which files should not go in
Before packing, it is worth thinking through one question: is there anything in this package that should not reach the other side? The most dangerous is secrets: a .env file holds API keys and database addresses, and packing it into a zip and sending it is the same as mailing the keys along with the house. The environment-variables guide said it already — keys go into the environment, never into the repository — and the same rule applies to zip packages: before packing, check for .env files, config files with passwords, and private keys, and keep them all out. zip -x can exclude them, but the most reliable way is to look at the directory you are about to pack and confirm nothing is in there that should not be.
The second category is things the machine generated itself: a dependency directory like node_modules can be reinstalled by the recipient, and sending it makes the package dozens of times bigger and risks mismatched versions; __pycache__, the .git directory, and .DS_Store files are the same — the recipient cannot use them, and they carry traces of your machine along. The third category is files that are already compressed: photos, videos, and pre-compressed installers barely shrink when packed again, and only waste packing time. Spend one minute before packing on a short list: the files to send, the directories to exclude, and the secrets that must stay out. Confirm the list, then run zip -r.
7. Extract just one file: add the filename to unzip, and -d for where it goes
Sometimes a package holds a hundred files and you want just one: unzip -o release.zip "project/index.html" -d out. The -o means "overwrite a file of the same name without stopping to ask", and -d out means "unpack into the out directory". In the test run the output is inflating: out/project/index.html, and the file lands at out/project/index.html. Comparing it with cmp project/index.html out/project/index.html prints nothing and exits with code 0 — the two files are identical; what came out is the file that went in.
For extracting one file, the filename must be the full path as stored in the package, project/index.html — not just index.html. Write it wrong and unzip reports that the file cannot be found, because the name does not match, not because the file is missing from the package. The -d is worth making a habit too: without a directory, unpacked files spread across the current directory; with one, everything is collected inside that directory, and cleaning up means deleting a single directory.
8. Deleting entries from a package: zip -d removes without unpacking
If the wrong file got into a package, you do not need the three-step dance of unpacking, deleting, and repacking: zip -d release.zip project/data/notes.txt removes it from the package directly. In the test run the output is deleting: project/data/notes.txt, and the listing afterwards shows 9 files and 9621 bytes — the 42-byte notes.txt is gone and everything else is still there. That command fits the last-minute tidy-up before sending: a draft file or a screenshot slipped into the package, one zip -d and it is gone.
Deleting entries needs the full path as stored in the package too, for the same reason as extracting one file in the last section. Check the path with unzip -l before deleting — a package has no recycle bin for a file deleted by mistake. The safe way is to copy the package first: cp release.zip release.bak.zip, then delete. Package files are small, so a copy costs almost nothing, while a mistake costs a package that has to be rebuilt.
9. The absolute-path pitfall: zip quietly eats the leading slash
The second real pitfall in this guide is about paths. In the test run we packed with an absolute path: zip abs.zip /tmp/demo277-zip/project/index.html. The output is adding: tmp/demo277-zip/project/index.html (deflated 15%) — look closely: the path in the output no longer starts with /tmp. zip quietly ate the leading slash, and what went into the package is tmp/demo277-zip/project/index.html. The listing confirms it: the package holds one file of 80 bytes, at the path tmp/demo277-zip/project/index.html.
zip does this for a good reason: an absolute path unpacks to a fixed place on the system, and a package from someone else could overwrite system files on unpacking, so zip simply does not store absolute paths. But it eats the slash without a word — no error, no warning — and unless you read the listing you will never know the path inside the package changed. The correct way is to cd into the directory you want to pack first and use a relative path: zip -r release.zip project, so the paths inside the package start cleanly with project/, and where they land on unpacking is entirely the unpacker's decision.
10. The no -d pitfall: unpacking spreads files across the current directory
The third pitfall is on the unpacking side. In the test run we made an empty directory called pollute, went inside it, and ran unzip -o ../release.zip with no -d. The output is a run of creating: project/, extracting: project/style.css, inflating: project/index.html, and ls afterwards shows a project directory in the current directory. The paths inside the package started with project/, so the files were collected into that directory — we were lucky. If the paths in a package are loose, with no single top-level directory, one unpacking scatters dozens of files straight into the current directory, and tidying up means picking them out one by one.
So the rule for unpacking matches the rule for packing: look at the listing first and confirm the package has a single top-level directory, then add -d and unpack into a fresh empty directory. One -d in unzip -o release.zip -d out saves the half hour of cleaning scattered files afterwards. With a package from someone else, run unzip -l first: the messier the paths in the listing, the more reason to unpack into an empty directory, look at what came out, and only then decide where to move it.
11. Compression levels and quiet mode: two switches hiding in zip -h
The zip help text, zip -h, lists two switches worth knowing for everyday use; the test run did not exercise them, but they are worth knowing. The first is the compression level: -0 stores without compressing, -1 compresses faster and less, -9 compresses harder and slower, and with no flag you get the default middle level. Most of the time you can ignore it — the default is enough. It is worth adjusting in only two situations: packing a large directory in a hurry, where -1 or -0 gets the package out first; or squeezing a package as small as possible for sending, where -9 is worth the extra wait. The second is -q, quiet mode: packing without printing an adding line per file, for scheduled packing in scripts where the log should not be flooded. There is also -T, which tests the package's integrity right after packing — the same effect as unzip -t in section 4, done in one step so you do not need a second command.
These two switches also make a point: you do not need to memorise zip's options. When you forget, run zip -h and look — the help is a single screen, and the most-used options, -r, -q, -x, -d, and -0 to -9, are all on it. The only things really worth remembering are the three steps — pack, list, test — and the options can be looked up as you use them, forgotten, and looked up again next time without any shame.
12. zip is not a safe: on encryption, and where tar fits
Finally, two things zip cannot do. First, zip has a -e option to put a password on a package, using the traditional ZipCrypto encryption, which is weak — current tools can brute-force it in minutes. Files that genuinely need to stay secret should not rely on a zip password; use a proper encryption tool, or an encrypted channel for sending them. And never put a password on the command line: it stays in shell history, where anyone reading the history can see it. Second, zip and tar are not the same thing: tar only packs and never compresses, and is usually paired with gzip; zip packs and compresses in one pass, and Windows and macOS unpack it natively. Sending to a friend who does not know the command line, zip is the safer choice; backing up a large server directory, tar plus gzip is the more common combination. Each tool has its home ground, and knowing which one to reach for matters more than memorising options.
Test record: every step above was run for real on Zip 3.0 and UnZip 6.00, and the full output is in demo277_zip.sh and demo277_zip_transcript.txt (144 lines). The rules of packing really come down to three: pack with -r, look at the listing before unpacking, and always unpack with -d. Remember those three, and zip will stop causing you trouble.
Comments (0)
Sources
Related articles
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.
cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.