Linux Zip a Directory: Command and Practical Examples
In this article
To zip a directory in Linux, run zip -r archive.zip directory/. The -r option tells zip to include the directory’s files and subdirectories recursively; replace archive.zip and directory/ with the names you want.
The Basic Command
For example, to create a ZIP file named project.zip from a directory named project, use:
zip -r project.zip project/
Run the command from the directory that contains project. The resulting archive will normally contain entries beginning with project/, rather than placing the directory’s contents at the archive’s top level. That is often useful: when someone extracts the ZIP, the files stay together inside a folder.
You can also give the archive and source directory absolute or relative paths. For instance:
zip -r /home/ana/backups/project.zip /home/ana/work/project/
However, using an absolute source path may result in an archive with longer path entries, depending on the command and implementation. If you want a clean, predictable layout, change to the parent directory first and use a relative path:
cd /home/ana/work
zip -r /home/ana/backups/project.zip project/
Install and Check the zip Utility
The zip command is a separate utility and may not be installed by default. If the shell reports zip: command not found, install the package using your distribution’s package manager. Common examples include:
- Debian or Ubuntu:
sudo apt install zip - Fedora:
sudo dnf install zip - Arch Linux:
sudo pacman -S zip
Package names and installation policies can differ, particularly on older or specialized systems. You can check whether the command is available with zip -v. This prints version and build information; it does not create an archive.
What the Options Do
The most important option for a directory is -r, short for recursive. Without it, zip can archive individual files, but it will not walk through a directory tree as intended. The basic command has two main arguments: the output archive name and the input path.
Options can help tailor the archive. For example, -q suppresses most routine output, while -9 requests a higher compression level:
zip -r -9 project.zip project/
Higher compression can take more time and does not guarantee a smaller file. Already-compressed files—such as many JPEG images, video files, and existing ZIP archives—often shrink little. The default compression level is a reasonable choice for most everyday use.
To see the full set of options supported by your installed version, run zip --help or consult its manual with man zip. Details such as encryption support and option behavior can vary by version.
Choose What Goes Into the Archive
By default, recursive archiving includes files and subdirectories beneath the source directory, including hidden files that are part of the directory tree. The shell’s filename expansion is not used to enumerate those contents; zip traverses the directory because of -r.
To exclude common temporary or generated files, use an exclusion pattern. For example:
zip -r project.zip project/ -x '*/.git/*' '*/node_modules/*'
These patterns are interpreted by zip, not by the shell, so quote them to prevent the shell from expanding wildcard characters against files in your current directory. Check the resulting archive if exclusions matter: patterns should be tested against the paths that the archive actually contains.
If you only want selected files, you can pass their paths instead of archiving the whole directory. For instance, zip notes.zip project/README.md project/notes.txt creates an archive from those two files. To include several matching files, shell patterns can be useful, but remember that a pattern such as project/*.txt usually matches only entries in that directory, not text files in nested subdirectories.
Check, List, and Extract a ZIP
After creation, inspect the archive rather than assuming it contains exactly what you intended. Use -T to test its internal consistency, and -sf to list its file names:
zip -T project.zip
zip -sf project.zip
To extract the archive into the current directory, use:
unzip project.zip
To extract it somewhere else, specify a destination with -d:
unzip project.zip -d /tmp/project-copy
Be careful when extracting archives from unknown sources. Archive entries can contain paths that target locations beyond the intended extraction folder, and files may overwrite existing ones. Inspect unfamiliar archives and extract them into a new, unimportant directory rather than a directory containing valuable data.
Common Problems and Alternatives
If the archive is missing subfolders, check that you used -r and that the source path is correct. If you receive a permission error, the current user may not be allowed to read some of the source files or write the ZIP file to its destination. Avoid running the whole command with elevated privileges unless there is a specific need: doing so can create an archive containing files you did not intend to share.
If you run the command again with the same archive name, zip may update the existing archive rather than simply replacing it. To ensure a fresh archive, remove or rename the old one first, or choose a new output name. Take care with commands such as rm; verify the filename before deleting anything.
ZIP is widely supported across Linux, Windows, and macOS, which makes it convenient for sharing. For Linux backups or transfers where Unix permissions, symbolic links, ownership, or other filesystem details matter, a compressed tar archive may be a better fit. For example, tar -czf project.tar.gz project/ creates a gzip-compressed tar archive. ZIP and tar are different formats, so choose based on compatibility and what file metadata you need to preserve.
Frequently Asked Questions
How do I zip a directory without including the directory name?
Change into the directory and archive its contents with zip -r ../project.zip .. The archive will contain paths relative to the current directory, so files appear at its top level instead of under a project/ folder. Include hidden files deliberately if you use another method, since shell patterns such as * generally do not match them.
How do I create a ZIP file with a different name?
Put the desired archive name before the source path: zip -r backup-2026.zip project/. The archive extension is conventional; the name does not need to end in .zip, but using that extension makes the format clear to people and tools.
Does zip -r include hidden files?
When you give zip -r a directory, it recursively processes its entries, including hidden files and directories such as .config and .git. If you do not want particular hidden data included, exclude it explicitly and inspect the archive listing.
How do I password-protect a ZIP file?
Some zip versions support interactive encryption with zip -e -r protected.zip directory/, prompting for a password. Traditional ZIP encryption is not a strong choice for protecting sensitive information. For confidential data, use a well-reviewed encryption tool and share the password through a separate secure channel.
What is the difference between zip and tar.gz?
ZIP is broadly supported by desktop operating systems and combines archiving with compression. A .tar.gz file is a tar archive compressed with gzip and is common in Linux environments; it can be more suitable when preserving Unix-oriented file information matters. Neither format is universally better—the intended recipient and required metadata should guide your choice.