zip Command in Linux: Zip Files and Directories

When you need to share logs, configs, project files, or a whole directory with users on Linux, macOS, or Windows, ZIP is often the most practical archive format to choose. The zip command creates .zip archives from files and directories directly from the terminal.
A ZIP file is a data container that can hold one or more compressed files or directories. Compressed files take up less disk space and are easier to transfer between systems. This guide explains how to create ZIP archives in Linux using the zip command.
Quick Reference
For a printable quick reference, see the ZIP/UNZIP cheatsheet .
| Task | Command |
|---|---|
| Zip files | zip archive.zip file1 file2 |
| Zip a directory recursively | zip -r archive.zip directory/ |
| Zip all files in current directory | zip archive.zip * |
| Zip current directory, including hidden files | zip -r ../archive.zip . |
| Zip without compression | zip -0 archive.zip file1 file2 |
| Maximum compression | zip -9 -r archive.zip directory/ |
| Use bzip2 compression | zip -r -Z bzip2 archive.zip directory/ |
| Exclude files | zip -r archive.zip directory/ -x "*.log" |
| Password-protected archive | zip -e archive.zip file1 file2 |
| Split archive into 1GB parts | zip -s 1g -r archive.zip directory/ |
| Quiet mode (no output) | zip -q archive.zip file1 file2 |
Installing zip
zip
is a command-line utility that helps you create Zip archives. It is not installed by default in most Linux distributions, but you can install it using your distribution’s package manager.
Install zip on Ubuntu, Debian, and Derivatives
sudo apt install zipInstall zip on Fedora, RHEL, and Derivatives
sudo dnf install zipInstall zip on Arch Linux
sudo pacman -S zipZipping Files
To zip one or more files, specify the files you want to add to the archive separated by space:
zip archivename.zip filename1 filename2 filename3adding: filename1 (deflated 63%)
adding: filename2 (stored 0%)
adding: filename3 (deflated 38%)By default, the zip command prints the names of the files added to the archive and the compression method.
If the archive name does not end with .zip, the extension is added automatically. For example, zip archivename filename creates archivename.zip.
To suppress the output of the zip command, use the -q option:
zip -q archivename.zip filename1 filename2 filename3Zipping Directories
To zip a directory including the contents of its subdirectories, use the -r (recursive) option:
zip -r archivename.zip directory_nameYou can also add multiple files and directories in the same archive:
zip -r archivename.zip directory_name1 directory_name2 file1 file2Zipping the Current Directory
To zip everything in the current directory, including hidden files and directories, run zip from inside the directory and write the archive outside it:
zip -r ../archivename.zip .The dot (.) tells zip to include the current directory contents. Writing the archive to the parent directory avoids adding the new archive file back into itself while the command is running.
Excluding Files from a Zip Archive
To skip files or directories, use the -x option followed by one or more patterns:
zip -r archivename.zip directory_name -x "*.log" "*.tmp"The command above creates an archive from directory_name but excludes files ending in .log and .tmp.
You can also exclude a directory by matching its path:
zip -r archivename.zip directory_name -x "directory_name/cache/*"Compression Methods and Levels
The default compression method of Zip is deflate. If the zip utility determines that a file cannot be compressed, it simply stores the file in the archive without compressing it using the store method. In most Linux distributions, the zip utility also supports the bzip2 compression method.
To specify a compression method, use the -Z option:
zip -r -Z bzip2 archivename.zip directory_name...
adding: sub_dir/ (stored 0%)
adding: sub_dir/file1 (bzipped 52%)
adding: sub_dir/file2 (bzipped 79%)The zip command allows you to specify a compression level using a number prefixed with a dash from 0 to 9. The default compression level is -6. When using -0, all files will be stored without compression. -9 will force the zip command to use optimal compression for all files.
For example, to use the compression level -9, you would type:
zip -9 -r archivename.zip directory_nameThe higher the compression level, the more CPU-intensive the zip process is, and it will take more time to complete.
Creating a Password-Protected Zip File
To create an encrypted archive, use the -e option:
zip -e archivename.zip directory_nameYou will be prompted to enter and verify the archive password:
Enter password:
Verify password:The -e option uses traditional ZIP encryption. It is useful for simple sharing, but it is not the right choice for highly sensitive data. For stronger encryption, use a dedicated tool such as gpg or age.
Creating a Split Zip File
Imagine you want to store the Zip archive on a file hosting service that has a file size upload limit of 1GB, and your Zip archive is 5GB.
You can create a new split Zip file using the -s option followed by a specified size. The multiplier can be k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes):
zip -s 1g -r archivename.zip directory_nameThe command above will keep creating new archives in a set after it reaches the specified size limit:
archivename.zip
archivename.z01
archivename.z02
archivename.z03
archivename.z04Ownership and Permissions
Zip files do not support Linux-style ownership information. The extracted files are owned by the user that runs the command. To preserve the file ownership
and permissions, use the tar
command instead.
Common Options
The zip command accepts several options:
-r- Recurse into directories.-q- Quiet mode, suppress output.-e- Encrypt the archive with a password.-s size- Create a split archive with the specified segment size.-Z method- Set the compression method (deflate,store,bzip2).-0to-9- Set the compression level (0 = store only, 9 = maximum compression).-x pattern- Exclude files matching the pattern.-j- Store just the file names, without directory paths.-u- Update existing entries in the archive if the file on disk is newer.
Troubleshooting
zip: command not found
Install the zip package using your distribution package manager.
Permission denied
You do not have write permission in the target directory. Run the command in a directory you own or use sudo when appropriate.
FAQ
How do I zip a directory in Linux?
Use zip -r archive.zip directory_name. The -r option tells zip to recurse into the directory and include all files and subdirectories.
How do I unzip a file in Linux?
Use the unzip
command: unzip archive.zip. To extract to a specific directory, use unzip archive.zip -d /path/to/directory.
Does zip preserve file permissions?
No. Zip archives do not store Linux-style ownership or permissions. If you need to preserve these, use the tar command instead.
How do I exclude files from a zip archive?
Use the -x option followed by the pattern. For example, to exclude all .log files: zip -r archive.zip directory/ -x "*.log".
What is the difference between zip and gzip?zip creates an archive that can contain multiple files and directories. gzip compresses a single file and is typically used together with tar (as .tar.gz) to archive multiple files.
Conclusion
Use zip archive.zip file1 file2 for a few files and zip -r archive.zip directory/ when you need to include a directory tree. To extract a ZIP archive, use the unzip
command. If you need to preserve Linux ownership and permissions, use tar
instead.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page