How to Use zip in Linux
Learn to use Linux's zip command for bundling files, creating secure archives, and managing backups effectively.
The zip command in Linux is a straightforward utility used for packaging and compressing (or ‘zipping’) files and directories into a single, smaller file, typically with a .zip extension.
This is especially useful when you need to transport or backup multiple files or directories, as they can be bundled together into one .zip file. The zip
command also supports password protection, which can provide a basic level of security for your zipped files.
It’s worth mentioning that there are several other commands for compressing and packaging files in Linux, such as tar
and gzip
.
Here are some ways to use the zip
command:
1. Create a Zip Archive
The basic syntax for creating a zip
archive is zip archive_name file_name
.
Example:
To create a zip archive named archive.zip
that contains a file named file.txt
, you would use:
zip archive.zip file.txt
2. Add Multiple Files to a Zip Archive
You can add multiple files to a zip archive by specifying multiple file names.
Example:
zip archive.zip file1.txt file2.txt file3.txt
3. Add a Directory to a Zip Archive
You can add a directory and all its contents to a zip archive using the -r
(or --recurse-paths
) option.
Example:
zip -r archive.zip directory
4. Add Files to an Existing Zip Archive
You can add files to an existing zip archive with the same zip
command.
Example:
zip archive.zip newfile.txt
5. Exclude Files
If you want to add a directory but exclude certain files, you can use the -x
option.
Example:
zip -r archive.zip directory -x *.jpg
This command adds the directory to archive.zip
but excludes all .jpg
files.
6. Create a Password-Protected Zip Archive
You can create a password-protected zip archive using the -e
option.
Example:
zip -e archive.zip file.txt
After running this command, you will be prompted to enter and verify a password.
7. Display the Progress
If you’re compressing a large file or directory, you might want to display the progress. You can do this with the -v
(or --verbose
) option.
Example:
zip -rv archive.zip directory
Compressing Files: zip
vs tar
vs gzip
,
tar
, and gzip
are all Linux commands capable for compressing files, but they work in slightly different ways and have different use cases:
zip
zip
creates compressed archives that are entirely self-contained with the compression and archiving done at the same time.
This means zip
can compress multiple files into a single .zip file without needing any other tools. zip files are widely used and recognized, not only in Linux but also in Windows and macOS.
Another distinctive feature of zip
is that it supports password-based encryption for files, providing a basic level of security.
tar
tar
(Tape Archive) on the other hand, unlike zip
and gzip
, tar itself does not compress files; it’s used to bundle multiple files and directories into a single .tar file (also known as a tarball). This makes file management easier, especially for backup and transport.
The tar
command is often used in conjunction with compression utilities like gzip
to create compressed archives.
Related: How to use tar
in Linux
gzip
gzip
(GNU zip) only compresses a single file or stream of data. If you need to compress multiple files or directories with gzip
, you’ll typically use tar first to bundle everything into a single file, and then compress the tarball with gzip
, resulting in a .tar.gz file.
To sum things up:
-
zip
is a handy all-in-one tool for creating compressed archives, particularly when cross-platform compatibility or encryption is needed. -
tar
is great for bundling multiple files and directories, and -
gzip
is often used in combination with tar for compressing those bundles.
Each utility has its strengths and typical use cases, so the choice often depends on your specific needs.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |