How to use the command 'zip' (with examples)
- Linux
- December 25, 2023
The zip
command is used to package and compress (archive) files into a zip file. It is commonly used to bundle files and directories together, making it easier to transfer or store multiple files as a single file. The zip
command provides various options to customize the compression, encryption, and splitting of the archive file.
Use case 1: Add files/directories to a specific archive
Code:
zip -r path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
Motivation: Adding files or directories to a specific archive is useful when you want to combine multiple files into a single compressed file. This can be helpful for sharing or backup purposes, as it reduces the number of files to manage.
Explanation:
zip
: The command itself.-r
: This option tellszip
to recursively include all files and subdirectories within the specified directories.path/to/compressed.zip
: The path to the output compressed zip file.path/to/file_or_directory1 path/to/file_or_directory2 ...
: The paths to the files or directories you want to add to the archive.
Example output:
updating: path/to/file1.txt (stored 0%)
updating: path/to/directory/file2.txt (deflated 15%)
Use case 2: Remove files/directories from a specific archive
Code:
zip --delete path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
Motivation: Removing files or directories from a specific archive is useful when you want to update the contents of an existing compressed file. Instead of re-creating the whole archive, you can simply remove the unwanted files or directories.
Explanation:
zip
: The command itself.--delete
: This option tellszip
to delete the specified files or directories from the archive.path/to/compressed.zip
: The path to the compressed zip file.path/to/file_or_directory1 path/to/file_or_directory2 ...
: The paths to the files or directories you want to remove from the archive.
Example output:
deleting: path/to/file.txt
deleting: path/to/directory/
Use case 3: Archive files/directories excluding specified ones
Code:
zip path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ... --exclude path/to/excluded_files_or_directories
Motivation: Excluding specified files or directories while creating an archive can be useful when you want to include most of the files and directories, but there are a few specific ones that you want to exclude.
Explanation:
zip
: The command itself.path/to/compressed.zip
: The path to the output compressed zip file.path/to/file_or_directory1 path/to/file_or_directory2 ...
: The paths to the files or directories you want to add to the archive.--exclude
: This option tellszip
to exclude the specified files or directories from the archive.path/to/excluded_files_or_directories
: The paths to the files or directories you want to exclude from the archive.
Example output:
updating: path/to/file1.txt (stored 0%)
skipping: path/to/excluded_file.txt
updating: path/to/file2.txt (deflated 10%)
Use case 4: Archive files/directories with a specific compression level
Code:
zip -r -0-9 path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
Motivation:
By default, zip
uses a moderate compression level. However, in some scenarios, you may want to specify a different compression level. A higher compression level results in a smaller file size but may take more time to compress.
Explanation:
zip
: The command itself.-r
: This option tellszip
to recursively include all files and subdirectories within the specified directories.-0-9
: This option specifies the compression level, where0
means “no compression” and9
means “maximum compression”.path/to/compressed.zip
: The path to the output compressed zip file.path/to/file_or_directory1 path/to/file_or_directory2 ...
: The paths to the files or directories you want to add to the archive.
Example output:
updating: path/to/file1.txt (deflated 0%)
updating: path/to/file2.txt (deflated 5%)
Use case 5: Create an encrypted archive with a specific password
Code:
zip -r --encrypt path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
Motivation: Creating an encrypted archive adds an additional layer of security to your compressed file. It ensures that only those who know the password can access the contents of the archive.
Explanation:
zip
: The command itself.-r
: This option tellszip
to recursively include all files and subdirectories within the specified directories.--encrypt
: This option enables encryption for the archive.path/to/compressed.zip
: The path to the output compressed zip file.path/to/file_or_directory1 path/to/file_or_directory2 ...
: The paths to the files or directories you want to add to the archive.
Example output:
updating: path/to/file1.txt (stored 0%)
updating: path/to/file2.txt (deflated 10%)
Use case 6: Archive files/directories to a multi-part split zip file
Code:
zip -r -s 3g path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...
Motivation: Creating a multi-part split zip file can be useful when you want to split a large archive into smaller chunks. This can be helpful for easy distribution or storage on devices with limited file size support.
Explanation:
zip
: The command itself.-r
: This option tellszip
to recursively include all files and subdirectories within the specified directories.-s 3g
: This option splits the zip file into parts of 3 GB each.path/to/compressed.zip
: The path to the output compressed zip file.path/to/file_or_directory1 path/to/file_or_directory2 ...
: The paths to the files or directories you want to add to the archive.
Example output:
updating: path/to/file1.txt (stored 0%)
(part 1 95%)
updating: path/to/file2.txt (deflated 15%)
(part 2 35%)
Use case 7: Print a specific archive contents
Code:
zip -sf path/to/compressed.zip
Motivation: Viewing the contents of an archive can be helpful when you want to check the files and directories included in the zip file.
Explanation:
zip
: The command itself.-sf
: This option tellszip
to display the archive’s contents without extracting them.path/to/compressed.zip
: The path to the compressed zip file.
Example output:
Length Date Time Name
--------- ---------- ----- ----
0 2022-01-01 12:00 path/to/directory/
8 2022-01-01 12:00 path/to/file.txt
--------- -------
8 2 files
Conclusion:
The zip
command is a versatile tool for packaging and compressing files and directories into zip archives. The various options and use cases covered in this article allow you to customize the compression, encryption, and splitting of your archive files. Whether you need to bundle files for sharing, backup purposes, or simply improve storage efficiency, the zip
command provides a convenient solution.