Linux zip command
The Linux zip command is used to compress files.
zip is a widely used compression program; compressed files have the extension .zip.
Unlike gzip or bzip2, zip can compress multiple files or an entire directory and preserve the directory structure.
zip is also widely supported across platforms (such as Windows and macOS).
Syntax
zip [options] output.zip file1 file2 ...
output.zip: The name of the generated compressed file.file1 file2 ...: The file or directory to compress.
options (parameter options):
-r: Recursively compress all files in a directory and its subdirectories.-e: Set password protection for the compressed file.-q: Quiet mode, do not display the compression process.-v: Display detailed compression progress.-x: Exclude certain files or directories from compression.-m: Delete the original files after compression.-0to-9: Specify the compression level,-0indicates storage with no compression,-9indicates the highest compression ratio; the default is-6.
Example
Compress a single file
zip archive.zip example.txt
This command compresses example.txt into archive.zip.
** Compress multiple files**
zip archive.zip file1.txt file2.txt file3.txt
This command compresses file1.txt, file2.txt, and file3.txt into archive.zip.
** Recursively compress a directory**
zip -r archive.zip directory/
This command recursively compresses all files in the directory and its subdirectories, preserving the directory structure.
** Compress and set password protection**
zip -e archive.zip file.txt
This command compresses file.txt and sets password protection; a password must be entered when extracting.
** Exclude specific files**
zip -r archive.zip directory/ -x "*.log"
This command compresses all files under the directory/ directory, but excludes all .log files.
** Delete original files after compression**
zip -m archive.zip file.txt
This command compresses file.txt into archive.zip and deletes the original file file.txt.
** Decompress a file**
Use the unzip command to extract .zip files:
unzip archive.zip
This extracts the archive.zip file into the current directory, preserving the original directory structure and files.
Linux Command Reference