If you’ve done any work in the Microsoft Windows world, no doubt you’ve used zip files. It became such a popular feature that Microsoft eventually incorporated it into the Windows operating system starting with XP. The zip utility allows you to easily compress large files (both text and executable) into smaller files that take up less space.
To view the available options in the zip command run the below command:
% zip --help Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license. Zip 3.0 (July 5th 2008). Usage: zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list] The default action is to add or replace zipfile entries from list, which can include the special name - to compress standard input. If zipfile and list are omitted, zip compresses stdin to stdout. -f freshen: only changed files -u update: only changed or new files -d delete entries in zipfile -m move into zipfile (delete OS files) -r recurse into directories -j junk (don't record) directory names -0 store only -l convert LF to CR LF (-ll CR LF to LF) -1 compress faster -9 compress better -q quiet operation -v verbose operation/print version info -c add one-line comments -z add zipfile comment -@ read names from stdin -o make zipfile as old as latest entry -x exclude the following names -i include only the following names -F fix zipfile (-FF try harder) -D do not add directory entries -A adjust self-extracting exe -J junk zipfile prefix (unzipsfx) -T test zipfile integrity -X eXclude eXtra file attributes -y store symbolic links as the link instead of the referenced file -e encrypt -n don't compress these suffixes -h2 show more help
If you encounter an error shown below while running the zip command:
zip: command not found
you may try installing the zip package as shown below as per your choice of distribution.
Distribution | Command |
---|---|
OS X | brew install zip |
Debian | apt-get install zip |
Ubuntu | apt-get install zip |
Alpine | apk add zip |
Arch Linux | pacman -S zip |
Kali Linux | apt-get install zip |
CentOS | yum install zip |
Fedora | dnf install zip |
Raspbian | apt-get install zip |
zip Command Examples
1. If you try to compress a directory with other directories in it, the commands you are currently using will skip over those directories. You need to tell zip to recursively go into those folders using the -r option.
$ zip -r test.zip /tmp/test/*
2. Add files/directories to a specific archive:
$ zip -r path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
3. Remove files/directories from a specific archive:
$ zip -d path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
4. Archive files/directories e[x]cluding specified ones:
$ zip path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ... -x path/to/excluded_files_or_directories
5. Archive files/directories with a specific compression level (`0` – the lowest, `9` – the highest):
$ zip -r -0-9 path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
6. Create an [e]ncrypted archive with a specific password:
$ zip -r -e path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
7. Archive files/directories to a multi-part [s]plit zip file (e.g. 3 GB parts):
$ zip -r -s 3g path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
8. Print specific archive contents:
$ zip -sf path/to/compressed.zip