zip Command in Linux: Zip Files and Directories

By 

Updated on

8 min read

Zip Files and Directories in Linux

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.

The zip command takes the name of the archive first, followed by the files and directories you want to add:

txt
zip [OPTIONS] archive.zip file1 file2 ...

Quick Reference

For a printable quick reference, see the ZIP/UNZIP cheatsheet .

TaskCommand
Zip fileszip archive.zip file1 file2
Zip a directory recursivelyzip -r archive.zip directory/
Zip all files in current directoryzip archive.zip *
Zip current directory, including hidden fileszip -r ../archive.zip .
Add or update files in an archivezip archive.zip newfile
Remove a file from an archivezip -d archive.zip file1
Store files without directory pathszip -j archive.zip dir/file1
Store symbolic links instead of their targetszip -y -r archive.zip directory/
Zip without compressionzip -0 archive.zip file1 file2
Maximum compressionzip -9 -r archive.zip directory/
Use bzip2 compressionzip -r -Z bzip2 archive.zip directory/
Exclude fileszip -r archive.zip directory/ -x "*.log"
Password-protected archivezip -e archive.zip file1 file2
Split archive into 1GB partszip -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

Terminal
sudo apt install zip

Install zip on Fedora, RHEL, and Derivatives

Terminal
sudo dnf install zip

Install zip on Arch Linux

Terminal
sudo pacman -S zip

Zipping Files

To zip one or more files, specify the files you want to add to the archive separated by space:

Terminal
zip archivename.zip filename1 filename2 filename3
output
  adding: 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:

Terminal
zip -q archivename.zip filename1 filename2 filename3

Zipping Directories

To zip a directory including the contents of its subdirectories, use the -r (recursive) option:

Terminal
zip -r archivename.zip directory_name

You can also add multiple files and directories in the same archive:

Terminal
zip -r archivename.zip directory_name1 directory_name2 file1 file2

When the directory contains symbolic links, zip follows them by default and stores a copy of the file each link points to. If you want the archive to keep the links themselves, add the -y option:

Terminal
zip -y -r archivename.zip directory_name

Zipping 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:

Terminal
zip -r ../archivename.zip .

The dot (.) tells zip to include the current directory contents. The zip utility skips the archive it is writing, so the archive does not end up inside itself, but writing it to the parent directory keeps the archive out of the directory tree you are packing and out of the way if you run the command again.

Adding Files to an Existing Archive

You do not have to rebuild an archive from scratch to change what it holds. Running zip against an archive that already exists adds the new files and refreshes the ones that changed:

Terminal
zip archivename.zip newfile subdir/anotherfile
output
  adding: newfile (deflated 41%)
updating: subdir/anotherfile (stored 0%)

The output tells you what happened to each entry. Files that were not in the archive show up as adding, while files that were already there show up as updating.

If you only want to refresh entries that are already in the archive, use the -u (update) option. It compares the timestamps and rewrites an entry only when the file on disk is newer, so nothing new is added:

Terminal
zip -u archivename.zip subdir/anotherfile

Removing Files from an Archive

To delete entries from an archive, use the -d option followed by the paths as they appear inside the archive:

Terminal
zip -d archivename.zip subdir/anotherfile
output
deleting: subdir/anotherfile

The paths must match the ones stored in the archive, not the paths on disk. If you are not sure how an entry is named, list the archive contents first with unzip -l archivename.zip.

Excluding Files from a Zip Archive

To skip files or directories, use the -x option followed by one or more patterns:

Terminal
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:

Terminal
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:

Terminal
zip -r -Z bzip2 archivename.zip directory_name
output
...
  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:

Terminal
zip -9 -r archivename.zip directory_name

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

Terminal
zip -e archivename.zip directory_name

You will be prompted to enter and verify the archive password:

output
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):

Terminal
zip -s 1g -r archivename.zip directory_name

The command above will keep creating new archives in a set after it reaches the specified size limit:

output
archivename.zip
archivename.z01
archivename.z02
archivename.z03
archivename.z04

Ownership 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).
  • -0 to -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.
  • -d - Delete entries from an existing archive.
  • -y - Store symbolic links as links instead of copying the files they point to.
  • -T - Test the integrity of the archive after writing it.

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.

How do I add a file to an existing zip archive?
Run zip archive.zip newfile. The zip command adds files that are not in the archive yet and updates the ones that changed, so you do not need to create the archive again.

How do I list the contents of a zip file?
Use unzip -l archive.zip. The output shows the size, timestamp, and stored path of every entry, which is also the fastest way to check how a path is named before deleting it with zip -d.

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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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