
One letter decides whether your archive holds a folder or holds nothing. The zip utility bundles a directory into a single file, but only when you pass -r. That flag tells zip to walk down through every subfolder and pull in what it finds. Leave it off and you get an empty folder entry. Here is the command and the flags worth knowing.
How to Zip a Directory in Linux With One Command
The command follows a fixed shape:
zip -r archive_name.zip directory_name/
Type it, press Enter, and the archive appears in your current working directory. Nothing gets moved or deleted. The original folder stays where it is.
What Each Part of the Linux Zip Directory Command Does
| Piece | Job it does |
|---|---|
| zip | Calls the packing utility. |
| -r | Walks down through the folder and picks up everything inside. |
| archive_name.zip | The name you give the new archive. |
| directory_name/ | The folder you want packed. |
Order stays fixed. The output name always comes before the source. Point that last item at any path you like, absolute or relative. The full zip command reference lists every switch the tool accepts.
Useful Variations of the Linux Zip Directory Command
The base form covers most days. Four tweaks cover the rest.
Names That Contain Spaces
Wrap them in quotes. Without quotes, your shell reads one name as two separate arguments.
zip -r "My Archive.zip" "My Folder/"
Zip Multiple Directories Into One File
Write them in a row, separated by spaces. Loose files can join the same line.
zip -r backup.zip folder1/ folder2/ file1.txt
Exclude Files From the Archive
The -x switch drops matching items. Pair it with a wildcard to skip a whole file type.
zip -r archive.zip directory_name/ -x "*.log"
Password Protect the Zip Directory
Add -e. Your terminal pauses and asks for a passphrase before it builds anything.
zip -er secure.zip directory_name/
When the archive needs opening again, our walkthrough on extracting into a chosen folder covers the reverse trip, and the unzip utility guide explains the preview and test flags.
How to Zip a Folder Without the Parent Folder Inside
Sometimes you want the contents at the archive root, not nested one level deep. Move into the folder first, then write the archive one level up.
cd directory_name/
zip -r ../archive.zip .
The dot means the current directory. Writing to ../ keeps the archive out of the tree you are packing, so it never tries to include itself.
Setting the Compression Level When You Zip a Directory
Numbers from 0 to 9 control how hard zip squeezes. Commandlinux.com reported typical compressed size for text files at each level, measured as a percentage of the original.
Most of the gain arrives by level 5. Moving from 5 to 9 trims three percentage points while CPU time climbs sharply. Level 1 finishes fastest and suits large media folders that barely compress anyway.
Fixing zip: command not found on Linux
Some systems ship without the utility. The shell prints an error saying it cannot locate zip. Nothing is broken. The package just has not been installed.
| Distribution family | Install line |
|---|---|
| Ubuntu, Debian | sudo apt install zip unzip |
| CentOS, RHEL, Fedora | sudo dnf install zip unzip |
Older RHEL boxes still use yum in place of dnf. Grabbing unzip in the same line saves a second trip later. Once it lands, run your -r command again and it goes through clean.
Zip or Tar: Which Fits a Linux Directory Better
ZIP compresses each file on its own, which makes it portable across Windows and macOS without extra software. TAR bundles first and compresses the bundle after, so it keeps Linux ownership and permission bits intact.
Backups that get restored on the same machine belong in tar. Our tar compression walkthrough covers the flag combinations, and .tar.gz extraction handles the other side. For a single file rather than a folder, gzip works on its own.
Quick Recap on the Linux Zip Directory Task
Three things matter. Use -r so nothing buried inside gets skipped. Quote any name holding spaces. Install the package first if the shell complains.
Prefer clicking over typing? The guide on building an archive from the terminal or desktop shows both routes. Once the archive checks out, this page on removing a directory covers safe cleanup of the source.
FAQs
How do I zip a directory in Linux?
Run zip -r archive.zip directory_name/ in your terminal. The -r flag pulls in every file and subfolder. The archive lands in your current working directory once the command finishes.
Does zip include subfolders automatically?
No. Without -r, zip stores only an empty directory entry and skips the contents. The recursive flag is what walks the tree and adds nested files to the archive.
What do Reddit users pick for Linux backups, zip or tar?
Reddit threads on r/linuxquestions lean toward tar for backups because it preserves ownership and permission bits. ZIP gets recommended when the archive travels to Windows or macOS machines.
Why does Reddit warn against zipping a folder into itself?
Reddit posts flag the recursive loop risk. Writing the archive inside the folder you are packing can bloat output. Write it to the parent directory with ../archive.zip instead.
How do I exclude a folder while zipping?
Add -x with a quoted pattern after the source: zip -r archive.zip project/ -x “project/tmp/*”. Quotes stop the shell expanding the glob before zip reads it.