Linux zip Command at a Glance
The Linux zip command compresses files and folders into a single .zip archive, and unzip extracts that archive again.
A ZIP archive stores each file compressed on its own, with a central directory at the end that lists every entry. That is why you can list or extract one file without unpacking the rest, and why the same archive opens on Windows and macOS without extra software.
| Task | Command |
|---|---|
| Zip one or more files | zip notes.zip a.txt b.txt |
| Zip a folder and everything in it | zip -r project.zip project |
| Zip a folder but skip some files | zip -r project.zip project -x '*.log' |
| Zip with a password | zip -er secret.zip folder |
| List an archive without extracting | unzip -l project.zip |
| Extract into a chosen folder | unzip project.zip -d restore |
| Extract without overwriting anything | unzip -n project.zip -d restore |
| Check an archive for damage | unzip -t project.zip |
| Split into parts of 100 MB | zip -r -s 100m backup.zip folder |
The steps apply to any Linux distribution that ships Info-ZIP zip 3.0 and UnZip 6.0, which covers current Ubuntu, Debian, Fedora, Rocky Linux and Arch releases. You need a shell, read access to the files you zip and write access to the folder where the archive goes. Root is only needed to install the packages.
zip is not the same as gzip. zip packs many files and folders into one archive. gzip compresses a single file, so on Linux it is paired with tar to build a .tar.gz archive, as compared further down.
Install zip and unzip on Linux
Install zip and unzip with your package manager, because many minimal Linux server images ship without one or both.
sudo apt update
sudo apt install zip unzipThat is the line for Ubuntu and Debian. Fedora, Rocky Linux and AlmaLinux use sudo dnf install zip unzip, and Arch Linux uses sudo pacman -S zip unzip. Those two lines follow each distribution package name and were not run for this guide. Confirm both tools are present and see their versions.
zip -v | head -2
unzip -v | head -1Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
This is Zip 3.0 (July 5th 2008), by Info-ZIP.
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.Those versions are old on paper but still the current releases, and distributions keep patching them. If you get zip: command not found after installing, open a new shell so it picks up the new program.
How to Zip a File in Linux
To zip a file in Linux, give zip the archive name first and then the file or files to put in it.
zip report.zip project/README.md project/src/module1.py adding: project/README.md (stored 0%)
adding: project/src/module1.py (deflated 80%)Each line shows one file and what happened to it. Deflated 80% means the file shrank by 80 percent with the default Deflate method. Stored 0% means zip saved the file without compression, because compressing it would not have made it smaller.
If the archive already exists, zip adds the new files to it, or replaces an entry with the same name, rather than starting over. The .zip ending is added for you if you leave it out. Paths are stored exactly as you type them, so zip report.zip project/README.md keeps the project/ folder inside the archive.
Zip Files Without Their Folder Paths
To zip files without their folder paths, add -j so every file sits at the top level of the archive.
zip -j flat.zip project/src/module1.py project/README.md adding: module1.py (deflated 80%)
adding: README.md (stored 0%)Two files with the same name in different folders would clash, so use -j only when the names are unique.
Zip Files From a List
Zip files from a list by piping names into zip -@, which reads one path per line from standard input.
find project -type f -name '*.py' | zip -@ py.zip adding: project/src/module2.py (deflated 80%)
adding: project/src/module3.py (deflated 80%)
adding: project/src/module1.py (deflated 80%)This is the cleanest way to zip files that match a condition, such as files changed in the last day with find -mtime -1. It handles names with spaces, but because it reads one name per line, avoid it when file names can contain a newline.
Zip an Entire Folder Recursively
To zip an entire folder, add -r so zip walks into every subfolder, or it stores only the empty folder entry.
The test folder for this guide holds Python files, logs, a node_modules folder, a .git folder, a .env file and one symbolic link, about 1.4 MB in all.
zip -r project.zip project adding: project/ (stored 0%)
adding: project/.env (stored 0%)
adding: project/src/ (stored 0%)
adding: project/src/module2.py (deflated 80%)
adding: project/src/module3.py (deflated 80%)
adding: project/src/module1.py (deflated 80%)
adding: project/src/photo.jpg (deflated 0%)
adding: project/README.md (stored 0%)
adding: project/.git/ (stored 0%)
adding: project/.git/HEAD (stored 0%)
adding: project/logs/ (stored 0%)
adding: project/logs/error.log (deflated 60%)
adding: project/logs/app.log (deflated 62%)
adding: project/node_modules/ (stored 0%)
adding: project/node_modules/lib/ (stored 0%)
adding: project/node_modules/lib/index.js (stored 0%)
adding: project/readme-link.md (stored 0%)Hidden files such as .env and .git are included when you name the folder itself. The JPEG barely shrinks, because photos, videos and existing archives are already compressed. Add -q to hide this list in scripts.
Why the Star Wildcard Misses Hidden Files
The star wildcard misses hidden files because the shell expands * before zip runs, and by default it skips names starting with a dot.
cd project
zip -rq ../star.zip *
unzip -l ../star.zip | grep -c '\.env\|\.git'0Zero matches means .env and .git were left out. Run zip -r ../all.zip . from inside the folder instead, or zip the folder by name from its parent, and the hidden files go in.
How zip Handles Symbolic Links
By default zip follows symbolic links and stores a copy of the file they point to. Add -y to store the link itself.
zip -ry links.zip project/readme-link.md project/README.md
zipinfo links.zipArchive: links.zip
Zip file size: 375 bytes, number of entries: 2
lrwxrwxrwx 3.0 unx 9 bx stor 26-Dec-03 15:48 project/readme-link.md
-rw-r--r-- 3.0 unx 10 tx stor 26-Dec-03 15:48 project/README.md
2 files, 19 bytes uncompressed, 19 bytes compressed: 0.0%The leading l in lrwxrwxrwx shows the entry is a link. Without -y the same entry shows as a normal 10 byte file. Use -y for backups you will restore on Linux. Check the extraction tool on the other side before storing links, because some Windows tools do not recreate Unix symbolic links.
Exclude Files From a Zip Archive
To exclude files from a zip archive, add -x followed by quoted patterns after the folder name.
zip -r clean.zip project -x 'project/node_modules/*' 'project/.git/*' '*.log' 'project/.env' adding: project/ (stored 0%)
adding: project/src/ (stored 0%)
adding: project/src/module2.py (deflated 80%)
adding: project/src/module3.py (deflated 80%)
adding: project/src/module1.py (deflated 80%)
adding: project/src/photo.jpg (deflated 0%)
adding: project/README.md (stored 0%)
adding: project/logs/ (stored 0%)
adding: project/readme-link.md (stored 0%)The archive dropped from 524,584 bytes to 347,323 bytes. Patterns match the path as stored in the archive, so folder patterns need the leading project/, while *.log matches log files at any depth because zip lets * cross slashes. You can list several patterns after one -x, or repeat -x.
Always Quote the Exclude Pattern
Always quote the exclude pattern, because an unquoted *.log can be expanded by the shell before zip ever sees it.
In this test a stray top.log sat in the current folder. The unquoted pattern became -x top.log, so every log inside the project went into the archive with no warning.
zip -rq bad.zip project -x *.log
unzip -Z1 bad.zip | grep '\.log$'project/logs/error.log
project/logs/app.log
project/x.logBefore an exclusion matters, such as keeping secrets out of an archive you will share, preview it with zip -sf or check the result with unzip -Z1 archive.zip, which prints one path per line.
Unzip Files in Linux Safely
To unzip files in Linux safely, list the archive first, then extract into an empty folder with -d.
unzip -l clean.zipArchive: clean.zip
Length Date Time Name
--------- ---------- ----- ----
0 2026-12-03 15:48 project/
0 2026-12-03 15:48 project/src/
248894 2026-12-03 15:48 project/src/module2.py
248894 2026-12-03 15:48 project/src/module3.py
248894 2026-12-03 15:48 project/src/module1.py
200000 2026-12-03 15:48 project/src/photo.jpg
10 2026-12-03 15:48 project/README.md
0 2026-12-03 15:48 project/logs/
10 2026-12-03 15:48 project/readme-link.md
--------- -------
946702 9 filesThe listing shows whether everything sits under one top folder or spills loose files, and how much space extraction needs. Without -d, unzip extracts into the current directory, so run pwd if you are unsure where that is. With -d, unzip creates the target folder if it is missing.
unzip -q clean.zip -d restoreControl What Happens to Existing Files
Existing files trigger a prompt by default, so pick -n to skip them or -o to replace them without asking.
replace restore/project/README.md? [y]es, [n]o, [A]ll, [N]one, [r]ename:That prompt stalls a script waiting for input. unzip -n never overwrites and is the safe default for automation. unzip -o overwrites everything silently, which the UnZip manual itself calls dangerous, so use it only when you mean to replace the files.
Extract One File or Flatten Folders
To extract one file, name it after the archive. To drop the folder structure, add -j and every file lands in the target folder.
unzip clean.zip project/README.md -d one
unzip -j clean.zip 'project/src/*.py' -d flatArchive: clean.zip
extracting: one/project/README.md
Archive: clean.zip
inflating: flat/module2.py
inflating: flat/module3.py
inflating: flat/module1.pyUnzip Several Archives at Once
To unzip several archives at once, quote the wildcard so the shell passes it unchanged and unzip matches the archive names itself.
unzip -q *.zip
unzip -q '*.zip' -d allcaution: filename not matched: links.zip
2 archives were successfully processed.Unquoted, the shell turned the line into unzip clean.zip links.zip, which asks for a file named links.zip inside clean.zip. Quoted, unzip processed both archives.
What unzip Does With Unsafe Paths
unzip does strip unsafe paths such as ../escaped.txt, the trick behind Zip Slip, which tries to write outside the target folder.
warning: skipped "../" path component(s) in ../escaped.txt
extracting: out/escaped.txt
extracting: out/ok.txtIn the test above it removed the ../ and kept the file inside the target folder. That shows how this UnZip version handled this one path. Do not assume every extractor, library or version handles malicious paths the same way, so still list untrusted archives first, extract them as a normal user into an empty folder, and never into / or a live web root.
Test a Zip Archive Before You Trust It
Test a zip archive with unzip -t, which decompresses every file and checks its CRC without extracting anything to disk.
unzip -t clean.zipArchive: clean.zip
testing: project/ OK
testing: project/src/ OK
testing: project/src/module2.py OK
testing: project/src/module3.py OK
testing: project/src/module1.py OK
testing: project/src/photo.jpg OK
testing: project/README.md OK
testing: project/logs/ OK
testing: project/readme-link.md OK
No errors detected in compressed data of clean.zip.The last line is the verdict, and the exit code is 0 when the test passes, which scripts can check. Use unzip -t for an archive that already exists. Add -T when creating or updating an archive to make zip test the resulting archive. Check the zip manual for the exact temporary file and replacement behavior before relying on it to protect an existing backup. On its own, zip -T clean.zip printed test of clean.zip OK.
A passing test proves the archive matches what zip wrote. It does not prove the archive holds every file you meant to include, so compare the listing with the source after any exclusion, and test a real restore of important backups.
Update or Remove Files in an Existing Zip
To update an existing zip, -u adds missing files and replaces entries whose source files are newer, while -d deletes named entries.
zip -u clean.zip project/README.md
zip -d clean.zip project/readme-link.mdupdating: project/README.md (stored 0%)
deleting: project/readme-link.md-u only looks at the files you name or the folders you recurse into. Files you deleted from disk stay in the archive. -FS syncs an archive with the disk and removes entries whose files are gone, so keep a copy of the archive before you use it.
Password Protect a Zip File in Linux
To password protect a zip file in Linux, add -e and type the password at the prompt, which hides it from the screen.
zip -er secret.zip project/src/module1.py project/README.mdEnter password:
Verify password:
adding: project/src/module1.py (deflated 80%)
adding: project/README.md (stored 0%)Know what this protects. Info-ZIP zip uses the traditional ZipCrypto method, which the zip manual itself calls relatively weak. 7-Zip reports the method on our test archive.
7z l -slt secret.zip | grep -E '^Path|^Encrypted|^Method'Path = secret.zip
Path = project/src/module1.py
Encrypted = +
Method = ZipCrypto Deflate
Path = project/README.md
Encrypted = +
Method = ZipCrypto StoreFile names, sizes and dates also stay readable without the password, as unzip -l secret.zip shows. ZipCrypto is weak legacy encryption and should not guard confidential data. Both the method and the strength of the password matter.
Avoid the -P Password Option
The -P password option puts the password on the command line, where shell history and process listings can expose it.
On most multi-user systems, other users who list running processes can see the full command line too.
Both the zip and unzip manuals mark it as insecure. Use the -e prompt instead, and for unattended jobs encrypt with a tool built for it, such as GnuPG with a key.
Stronger Encryption With 7-Zip
Stronger encryption comes from 7-Zip, whose 7z format uses AES-256 and can also hide file names with -mhe=on.
sudo apt install 7zip
7z a -p -mhe=on secret.7z project/src/module1.py project/README.mdUbuntu 24.04 ships the 7zip package from its Universe repository, while Ubuntu 22.04 uses the older p7zip-full, so check the name on your release. In our test, 7z l -slt reported Method = LZMA2:18 7zAES and refused to even list the file names without the password. The 7z format needs 7-Zip or a compatible tool on the other side. If the recipient must use a .zip file, 7z a -tzip -p -mem=AES256 file.zip folder makes an AES-256 zip. UnZip 6.0 on Ubuntu cannot open it, as our test showed with 1 file skipped because of unsupported compression or encoding, so the recipient still needs 7-Zip or a recent desktop tool. Encryption is only as strong as the password you choose.
Split a Large Zip Into Smaller Parts
To split a large zip into smaller parts, add -s with a size such as 100m, and zip writes numbered pieces plus a final .zip.
zip -rq -s 1m b2.zip big2
ls -l b2.z*-rw-r--r-- 1 root root 1048576 Dec 3 15:51 b2.z01
-rw-r--r-- 1 root root 1048576 Dec 3 15:51 b2.z02
-rw-r--r-- 1 root root 1048576 Dec 3 15:51 b2.z03
-rw-r--r-- 1 root root 550558 Dec 3 15:51 b2.zipThe test used 1 MB parts to keep it small. The minimum is 64 KB, and a number without a letter means megabytes. All parts must stay together in one folder, and the last part is always the one named .zip.
Extract a Split Zip Archive
UnZip 6.0 cannot extract a split zip archive directly, so join the parts into one file first or use 7-Zip.
zip -s 0 b2.zip --out joined.zip
unzip -t joined.zip | tail -1No errors detected in compressed data of joined.zip.In the test, 7-Zip also read the split set as it was with 7z x b2.zip, and the extracted files matched the originals byte for byte. Whichever way you choose, test the result before you delete the parts.
Files Larger Than 4 GB
Files larger than or equal to 4 GiB, or archives with 65,535 or more entries, need Zip64, which zip uses automatically then.
Zip64 extends the size and entry count limits of the original ZIP format. Info-ZIP zip uses it when an entry or the archive reaches those limits.
zip -q huge.zip huge.img
unzip -l huge.zip
zipinfo -v huge.zip | grep 'minimum software'Archive: huge.zip
Length Date Time Name
--------- ---------- ----- ----
4718592000 2026-12-03 15:51 huge.img
--------- -------
4718592000 1 file
minimum software version required to extract: 4.5The 4.5 GB test file was a sparse file full of zeros, so it compressed to about 4.6 MB in 23 seconds. Real data takes far longer and needs room for the archive, so check free disk space first. Version 4.5 is the Zip64 marker. UnZip 6.0 and current Windows and macOS tools open it, while some very old tools do not.
Zip vs tar.gz on Linux
Zip suits files you share with Windows or macOS users, while tar.gz or tar.xz suits Linux backups that must keep ownership and links.
We packed the same 1.4 MB of source files and logs, including one incompressible 200 KB image, with each format at its default settings.
| Format | Command | Size in bytes | Share of original |
|---|---|---|---|
| zip | zip -rq p.zip src logs | 523,035 | 37% |
| tar.gz | tar -czf p.tar.gz src logs | 526,026 | 37% |
| tar.zst | tar --zstd -cf p.tar.zst src logs | 280,227 | 20% |
| tar.xz | tar -cJf p.tar.xz src logs | 227,592 | 16% |
| 7z | 7z a p.7z src logs | 228,622 | 16% |
Zip and tar.gz both use Deflate and landed within 1 percent of each other. On this mixed test data, mostly text files plus one JPEG, tar.xz and 7z came out at less than half the size of the zip. Other files can give very different results, and photos and video barely shrink in any format.
gzip on its own compresses one file and replaces it. In the test, gzip app.log left only app.log.gz behind. To pack a folder, combine it with tar.
gzip app.log
tar -czf backup.tar.gz project| You need | Better choice | Why |
|---|---|---|
| To send files to Windows or macOS users | zip | Both systems open .zip with no extra software |
| To pull one file out of a large archive | zip | Each entry is compressed alone and listed in a directory |
| To back up a Linux server | tar.gz or tar.xz | tar keeps owners, groups and links by default |
| The smallest archive | tar.xz or 7z | LZMA beat Deflate by more than half here |
| Fast compression of big logs | tar.zst | zstd is built for speed with good ratios |
Does zip Keep Linux Permissions
Zip keeps Linux permission bits but not ownership unless you extract as root with unzip -X.
stat -c '%A %u:%g %n' perm/deploy.sh r1/perm/deploy.sh r2/perm/deploy.sh-rwxr-x--- 1001:1001 perm/deploy.sh
-rwxr-x--- 0:0 r1/perm/deploy.sh
-rwxr-x--- 1001:1001 r2/perm/deploy.shThe plain extract in r1 kept mode 750 but gave the file to root. The -X extract in r2 restored the archived UID and GID because it ran with sufficient privileges. Ownership restoration depends on the operating system and those privileges. For a Linux file backup that must keep ownership, permissions, symbolic links, ACLs and extended attributes, use GNU tar with tar --acls --xattrs -czf site.tar.gz project, and back up databases separately with their own backup tools. If you are moving a site to a new Linux VPS, a tar.gz of the web root plus a database dump is the usual pair.
zip Compression Levels
zip compression levels run from -0, which stores without compression, to -9, maximum compression effort, with -6 as the default.
| Level | Size in bytes |
|---|---|
-0 store only | 1,405,824 |
-1 fastest | 519,932 |
-6 default | 523,035 |
-9 best | 522,906 |
Higher levels can take longer without a meaningfully smaller archive. Every level from 1 to 9 landed within 1 percent here, and -1 was even slightly smaller on this repetitive text. That is normal for Deflate. Use -0 for media that will not shrink, and switch formats rather than levels when size really matters.
zip and unzip Options Reference
These zip and unzip options handle the common compression and extraction tasks. man zip and man unzip list the rest.
| Option | What it does |
|---|---|
-r | Include folders and everything below them |
-x pattern | Exclude matching paths, quoted |
-e | Encrypt with a password typed at a prompt |
-u | Add missing files and replace entries when source files are newer |
-d name | Delete an entry from the archive |
-m | Move files in and delete the originals |
-y | Store symbolic links as links |
-s size | Split into parts of that size |
-0 to -9 | Store only through best compression, default 6 |
-q | Quiet output |
-T | Test the resulting archive after creating or updating it |
-sf | Show the files that would be operated on, then exit |
-j | Store files without their folder paths |
-@ | Read file names from standard input |
| Option | What it does |
|---|---|
-l | List contents without extracting |
-t | Test every file against its CRC |
-d folder | Extract into that folder |
-n | Never overwrite existing files |
-o | Overwrite existing files without asking |
-j | Drop folder paths and extract flat |
-x pattern | Skip matching entries |
-q | Quiet output |
-X | Restore archived UID and GID when supported and permitted |
-Z1 | Print bare file names, one per line |
Fix Common zip and unzip Errors
Most zip and unzip errors come from a wrong path, an unquoted wildcard or a split archive opened the wrong way.
| What you see | Likely cause | Fix |
|---|---|---|
zip: command not found | The package is missing | Install zip and unzip with your package manager |
zip error: Nothing to do! and exit code 12 | No file matched the name you gave | Check the path and run ls on it |
caution: filename not matched | An unquoted wildcard in unzip | Quote it, as in unzip '*.zip' |
| Excluded files still in the archive | The -x pattern was not quoted or not anchored | Quote it and match the stored path |
| Folder zipped but empty | -r was missing | Run zip -r again |
bad zipfile offset on a split set | unzip cannot read split archives | Join with zip -s 0 or use 7z x |
replace ...? [y]es, [n]o stalls a script | Files already exist | Add -n or -o |
unable to get password | The archive is encrypted | Extract in a terminal and type the password |
Permission denied while zipping | You cannot read some files | Fix ownership or modes rather than running zip with sudo |
Running zip with sudo to get past a permission error can leave root-owned archives that normal users cannot update. Fix access to the files instead, and use sudo only when the files really belong to root.
Questions people ask
What is the zip command in Linux?
It is the Info-ZIP tool that packs files and folders into one compressed .zip archive that Windows and macOS can open too.
How do I zip an entire folder in Linux?
Run zip -r archive.zip folder. The -r option includes every file and subfolder, hidden ones too when you name the folder itself.
How do I zip a file in Linux?
Run zip archive.zip file.txt. Add more names to include several files, or run it again later to add a file to the same archive.
How do I unzip a file to a specific folder?
Run unzip archive.zip -d /path/to/folder. unzip creates the folder if it does not exist yet.
Is a password protected zip secure?
Only lightly. zip -e uses ZipCrypto, which is weak and leaves file names visible. Use 7-Zip with AES-256 for sensitive data.
Should I use zip or tar.gz on Linux?
Use zip to share files with Windows or macOS users. Use tar.gz or tar.xz for Linux backups that must keep owners and links.
What is the difference between zip and gzip?
zip packs many files and folders into one archive. gzip compresses one file, so tar is added to make a .tar.gz of a folder.
How do I see what is inside a zip without extracting it?
Run unzip -l archive.zip for names and sizes, or zipinfo archive.zip for permissions and compression methods too.
Sources
- zip(1) manual page, Info-ZIP zip 3.0
- unzip(1) manual page, Info-ZIP UnZip 6.0
- Ubuntu manpage, zip(1) split archives and filesync
- GNU tar manual, creating and reading compressed archives
- 7-Zip, 7z format and AES-256 encryption
- 7-Zip command line help, method switches including he and em
- Snyk Security Research, Zip Slip vulnerability