Linux zip Command
The Linux zip command is used to compress files.
zip is a widely used compression program, and the compressed file extension is.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 generated compressed filename.file1 file2 ...: The file or directory to compress.
options parameter options:
-r: Recursively compress all files in the directory and its subdirectories.-e: Set password protection for the compressed file.-q: Silent mode, does not display the compression process.-v: Display the detailed compression process.-x: Exclude certain files or directories from compression.-m: Delete the original files after compression.-0to-9: Specify the compression level,-0means store without compression,-9indicates the highest compression ratio, the default is-6。
Examples
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 is required to extract it.
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.
Extract files
Use the unzip command to extract .zip files:
unzip archive.zip
This will extract archive.zip to the current directory, preserving the original directory structure and files.
Other Extensions
Linux Command Reference