Skip to content

LinuxGuide

How to Zip and Unzip Files in Linux

  • Updated
  • 12 min read
  • VPS Lane Engineering
Short answer

Run zip -r archive.zip folder to zip a folder in Linux and unzip archive.zip -d target to extract it. Install both with apt.

How we tested this

Every command with output was run on Ubuntu 24.04.5 LTS as root with Info-ZIP zip 3.0 (package 3.0-13ubuntu0.3), UnZip 6.00 (6.0-28ubuntu4.1), GNU tar 1.35, gzip 1.12, xz 5.4.5, zstd 1.5.5 and 7-Zip 23.01. The dnf and pacman install lines were not run here and follow each distribution package name.

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.

The zip and unzip commands you will use most
TaskCommand
Zip one or more fileszip notes.zip a.txt b.txt
Zip a folder and everything in itzip -r project.zip project
Zip a folder but skip some fileszip -r project.zip project -x '*.log'
Zip with a passwordzip -er secret.zip folder
List an archive without extractingunzip -l project.zip
Extract into a chosen folderunzip project.zip -d restore
Extract without overwriting anythingunzip -n project.zip -d restore
Check an archive for damageunzip -t project.zip
Split into parts of 100 MBzip -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.

bash
sudo apt update
sudo apt install zip unzip

That 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.

bash
zip -v | head -2
unzip -v | head -1
Output
Copyright (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.

bash
zip report.zip project/README.md project/src/module1.py
Output
  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.

bash
zip -j flat.zip project/src/module1.py project/README.md
Output
  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.

bash
find project -type f -name '*.py' | zip -@ py.zip
Output
  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.

bash
zip -r project.zip project
Output
  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.

bash
cd project
zip -rq ../star.zip *
unzip -l ../star.zip | grep -c '\.env\|\.git'
Output
0

Zero 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.

By default zip follows symbolic links and stores a copy of the file they point to. Add -y to store the link itself.

bash
zip -ry links.zip project/readme-link.md project/README.md
zipinfo links.zip
Output
Archive:  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.

bash
zip -r clean.zip project -x 'project/node_modules/*' 'project/.git/*' '*.log' 'project/.env'
Output
  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.

bash
zip -rq bad.zip project -x *.log
unzip -Z1 bad.zip | grep '\.log$'
Output
project/logs/error.log
project/logs/app.log
project/x.log

Before 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.

bash
unzip -l clean.zip
Output
Archive:  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 files

The 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.

bash
unzip -q clean.zip -d restore

Control 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.

Output
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.

bash
unzip clean.zip project/README.md -d one
unzip -j clean.zip 'project/src/*.py' -d flat
Output
Archive:  clean.zip
 extracting: one/project/README.md   
Archive:  clean.zip
  inflating: flat/module2.py         
  inflating: flat/module3.py         
  inflating: flat/module1.py

Unzip 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.

bash
unzip -q *.zip
unzip -q '*.zip' -d all
Output
caution: 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.

Output
warning:  skipped "../" path component(s) in ../escaped.txt
 extracting: out/escaped.txt         
 extracting: out/ok.txt

In 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.

bash
unzip -t clean.zip
Output
Archive:  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.

bash
zip -u clean.zip project/README.md
zip -d clean.zip project/readme-link.md
Output
updating: 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.

bash
zip -er secret.zip project/src/module1.py project/README.md
Output
Enter 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.

bash
7z l -slt secret.zip | grep -E '^Path|^Encrypted|^Method'
Output
Path = secret.zip
Path = project/src/module1.py
Encrypted = +
Method = ZipCrypto Deflate
Path = project/README.md
Encrypted = +
Method = ZipCrypto Store

File 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.

bash
sudo apt install 7zip
7z a -p -mhe=on secret.7z project/src/module1.py project/README.md

Ubuntu 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.

bash
zip -rq -s 1m b2.zip big2
ls -l b2.z*
Output
-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.zip

The 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.

bash
zip -s 0 b2.zip --out joined.zip
unzip -t joined.zip | tail -1
Output
No 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.

bash
zip -q huge.zip huge.img
unzip -l huge.zip
zipinfo -v huge.zip | grep 'minimum software'
Output
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.5

The 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.

Measured archive sizes for the same 1,404,470 bytes on Ubuntu 24.04
FormatCommandSize in bytesShare of original
zipzip -rq p.zip src logs523,03537%
tar.gztar -czf p.tar.gz src logs526,02637%
tar.zsttar --zstd -cf p.tar.zst src logs280,22720%
tar.xztar -cJf p.tar.xz src logs227,59216%
7z7z a p.7z src logs228,62216%

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.

bash
gzip app.log
tar -czf backup.tar.gz project
zip or tar.gz, decided by what you need
You needBetter choiceWhy
To send files to Windows or macOS userszipBoth systems open .zip with no extra software
To pull one file out of a large archivezipEach entry is compressed alone and listed in a directory
To back up a Linux servertar.gz or tar.xztar keeps owners, groups and links by default
The smallest archivetar.xz or 7zLZMA beat Deflate by more than half here
Fast compression of big logstar.zstzstd 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.

bash
stat -c '%A %u:%g %n' perm/deploy.sh r1/perm/deploy.sh r2/perm/deploy.sh
Output
-rwxr-x--- 1001:1001 perm/deploy.sh
-rwxr-x--- 0:0 r1/perm/deploy.sh
-rwxr-x--- 1001:1001 r2/perm/deploy.sh

The 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.

zip compression levels on the same source and log files
LevelSize in bytes
-0 store only1,405,824
-1 fastest519,932
-6 default523,035
-9 best522,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.

Common zip options
OptionWhat it does
-rInclude folders and everything below them
-x patternExclude matching paths, quoted
-eEncrypt with a password typed at a prompt
-uAdd missing files and replace entries when source files are newer
-d nameDelete an entry from the archive
-mMove files in and delete the originals
-yStore symbolic links as links
-s sizeSplit into parts of that size
-0 to -9Store only through best compression, default 6
-qQuiet output
-TTest the resulting archive after creating or updating it
-sfShow the files that would be operated on, then exit
-jStore files without their folder paths
-@Read file names from standard input
Common unzip options
OptionWhat it does
-lList contents without extracting
-tTest every file against its CRC
-d folderExtract into that folder
-nNever overwrite existing files
-oOverwrite existing files without asking
-jDrop folder paths and extract flat
-x patternSkip matching entries
-qQuiet output
-XRestore archived UID and GID when supported and permitted
-Z1Print 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.

zip and unzip errors and what to do
What you seeLikely causeFix
zip: command not foundThe package is missingInstall zip and unzip with your package manager
zip error: Nothing to do! and exit code 12No file matched the name you gaveCheck the path and run ls on it
caution: filename not matchedAn unquoted wildcard in unzipQuote it, as in unzip '*.zip'
Excluded files still in the archiveThe -x pattern was not quoted or not anchoredQuote it and match the stored path
Folder zipped but empty-r was missingRun zip -r again
bad zipfile offset on a split setunzip cannot read split archivesJoin with zip -s 0 or use 7z x
replace ...? [y]es, [n]o stalls a scriptFiles already existAdd -n or -o
unable to get passwordThe archive is encryptedExtract in a terminal and type the password
Permission denied while zippingYou cannot read some filesFix 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

  1. zip(1) manual page, Info-ZIP zip 3.0
  2. unzip(1) manual page, Info-ZIP UnZip 6.0
  3. Ubuntu manpage, zip(1) split archives and filesync
  4. GNU tar manual, creating and reading compressed archives
  5. 7-Zip, 7z format and AES-256 encryption
  6. 7-Zip command line help, method switches including he and em
  7. Snyk Security Research, Zip Slip vulnerability

Written and tested by VPS Lane EngineeringWe run every step on real servers before publishing and update the guide when the software changes.