Zip a folder in Linux
In this post, I’ll show you how to zip folders in Linux with practical examples. I’ve been using zip for years to organize files, save disk space, and move data between servers.
The ZIP format has been around since 1989 (created by Philip Katz), and it’s still everywhere. You’ll find ZIP support on Linux, macOS, Windows—basically every operating system. It’s not the newest compression format, but it works and people actually know how to use it.
Now, let’s get practical. Here’s how you zip folders on Linux using the zip command.
Check if zip is installed
Most Linux systems have zip installed already. If yours doesn’t, here’s how to add it:
For Ubuntu, Mint, or other Debian-based distros:
sudo apt-get install -y zip unzip
For CentOS, Fedora:
sudo yum install -y zip unzip
How to zip a directory in Linux
The zip command syntax is straightforward. First, specify the zip file name you want to create. Then, list the files or folders you want to include, separated by spaces:
zip -r archive.zip file1 file2 folder1 folder2
The -r flag means recursive—it tells zip to include all subdirectories. Without it, you’d get an empty archive if you tried to zip a folder.
When you run the command, zip shows you what it’s adding:
adding: file1 (deflated 12%)
adding: file2 (stored 0%)
adding: folder1/ (stored 0%)
adding: folder1/file3 (deflated 45%)
If you don’t want this output, use -q for quiet mode:
zip -rq archive.zip folder1
Compression levels
Zip uses a default compression level of 6 (on a scale from 0-9). The higher the number, the more compression—but it takes longer and uses more CPU.
I usually stick with the default unless I have a specific reason to change it. Here’s when I adjust it:
- Level 0 (no compression): When I’m backing up already-compressed files like videos or images. Why waste CPU time trying to compress something that won’t shrink?
- Level 9 (maximum compression): When I’m archiving stuff for long-term storage and I don’t care how long it takes.
To specify a compression level, just add - followed by the number:
zip -9 -r backup.zip /home/user/documents
That uses maximum compression. For no compression at all:
zip -0 -r backup.zip /home/user/documents
Compression methods
Zip defaults to the deflate algorithm. You can use other methods with the -Z flag, but honestly? I rarely do this. The default works fine for most cases.
If you’re curious, here’s how to use bzip2 compression:
zip -r -Z bzip2 archive.zip foldername
In my experience, the difference isn’t usually worth the complexity. Stick with deflate unless you have a specific reason not to.
Zipping specific file types
Sometimes you want to grab just certain files from a directory. I do this a lot with log files or images. Here’s how to zip only PNG files:
find . -name "*.png" | xargs zip images.zip
This finds all PNG files in the current directory (and subdirectories) and pipes them to zip.
Updating existing zip files
Got a zip archive and need to replace one file inside it? Don’t unzip everything. Just use the update flag:
zip -uq backup.zip updated-file.txt
The -u flag updates the file if it exists, or adds it if it doesn’t. The -q keeps things quiet. I use this all the time when I’m maintaining script backups.
Password-protected zip files
Need to encrypt your zip file? Use the -e flag:
zip -e sensitive.zip *
It’ll prompt you for a password and ask you to confirm it. Anyone trying to unzip these files will need the password.
A quick note: the standard zip encryption isn’t particularly strong by modern standards. If you’re securing sensitive data, consider using gpg or another dedicated encryption tool instead.
Conclusion
That’s the zip command in practical terms. It’s not flashy, but it gets the job done. I use it regularly for quick backups, organizing files, and sending data to other people.
Keep in mind that zip isn’t your only option. In the Linux world, you’ll see a lot of tar.gz archives too. Tar.gz is more traditional for Unix systems, and I’ll cover that in another post.
For now, zip is a solid choice when you need compatibility across different operating systems—or when you’re working with people who aren’t comfortable with tar commands.
Comments