The zip command is a versatile tool for creating archives in Linux. In this tutorial, we’ll explore the zip command’s basic functionalities using some straightforward examples. Note that all examples in this guide have been tested on an Ubuntu 18.04 LTS system.
Linux Zip Command
The zip command in Linux is designed to package and compress (archive) files. Here’s the syntax:
zip [OPTIONS] archive_name list_of_files
According to the tool’s man page:
zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS/2, Windows 9x/NT/XP, Minix, Atari, Macintosh, Amiga, and Acorn RISC OS.
It is analogous to a combination of the Unix commands tar(1) and compress(1) and is compatible
with PKZIP (Phil Katz’s ZIP for MSDOS systems).
Let’s look into some commonly asked questions and walk through examples demonstrating how the zip command works.
FAQ on Using the Zip Command
Q1. How do I create a zip archive?
Creating a zip archive is simple. Provide the desired name for the archive followed by the files you want to compress:
zip files.zip file1.txt file2.txt file3.txt
This command compresses the three .txt files into a .zip archive named ‘files.zip’ in the current directory.
adding: file1.txt (stored 0%) adding: file2.txt (stored 0%) adding: file3.txt (stored 0%)
Q2. How do I remove a file from a zip archive?
To remove a file from an existing zip archive, use the -d option. For example, to delete file3.txt from files.zip:
zip -d files.zip file3.txt
This command outputs:
deleting: file3.txt
Q3. How do I add files to an existing zip archive?
Add new files to an existing archive using the -u option. For instance, to add file3.txt and file4.txt to files.zip:
zip -u files.zip file3.txt file4.txt
Output:
adding: file3.txt (stored 0%) adding: file4.txt (stored 0%)
Q4. How can I make zip delete the original files after archiving?
To delete the original files after creating the archive, use the -m option. For example:
zip -m files.zip file1.txt file2.txt file3.txt file4.txt
This command creates files.zip while removing the original .txt files from the directory.
Q5. What are some other useful zip command options?
-
- -x: Exclude specific files from compression. Example:
zip files.zip file2.txt
- This compresses all files in the current directory except file2.txt.
- -r: Recursive compression, useful for compressing entire directories along with their contents.
Conclusion
We’ve covered some basic functions of the zip command. By experimenting with the examples provided, you can become more acquainted with its use. For more advanced features, refer to the tool’s man page.
Frequently Asked Questions
- Can I create zip archives that span multiple disks? Yes, zip can create split archives that span multiple disks using the -s option to define the split size.
- Does zip support encryption? Yes, you can secure your archives with simple encryption using the -e option.
- Can I zip directories with subdirectories and files? Absolutely, by using the -r option, you can include all subdirectories and files within a directory.