Learn how to create and extract ZIP files from the Linux terminal with the zip and unzip commands.
Table of Contents
How to Install zip and unzip
The zip and unzip commands are the most common tools for creating and extracting ZIP files on Linux. They are usually installed by default on most Linux distributions, but you can check if they are available on your system by typing which zip and which unzip in the terminal. If you see a path to the executable file, such as /usr/bin/zip, then the command is installed. If you see nothing, then you need to install it.
To install zip and unzip on Ubuntu and Debian-based systems, use the following command:
sudo apt-get install zip unzip
To install zip and unzip on Fedora and CentOS, use the following command:
sudo dnf install zip unzip
or
sudo yum install zip unzip
depending on your version of the package manager.
How to Create a ZIP File with zip
To create a ZIP file with the zip command, you need to specify the name of the ZIP file and the files or folders that you want to include in it. For example, to create a ZIP file called my_files.zip that contains the files file1.txt, file2.txt, and file3.txt, use the following command:
zip my_files.zip file1.txt file2.txt file3.txt
You can also use wildcards to zip multiple files that match a certain pattern. For example, to zip all the files that have the .txt extension, use the following command:
zip my_files.zip *.txt
You can also zip an entire folder and its contents by using the -r option, which stands for recursive. For example, to zip the folder my_folder and all its subfolders and files, use the following command:
zip -r my_files.zip my_folder
How to Extract a ZIP File with unzip
To extract a ZIP file with the unzip command, you need to specify the name of the ZIP file that you want to unzip. For example, to extract the ZIP file my_files.zip, use the following command:
unzip my_files.zip
This will extract the files and folders to the current working directory. If you want to extract the files to a different directory, you can use the -d option, which stands for destination. For example, to extract the files to the directory my_destination, use the following command:
unzip my_files.zip -d my_destination
If the ZIP file is password-protected, you will be prompted to enter the password before the extraction. Alternatively, you can use the -P option to provide the password as an argument. For example, to extract the ZIP file my_files.zip with the password mypassword, use the following command:
unzip -P mypassword my_files.zip
Other Useful zip and unzip Options
The zip and unzip commands have many other options that can help you customize and optimize your ZIP operations. Here are some of the most useful ones:
- -l: List the contents of a ZIP file without extracting it.
- -v: Show verbose output, including the compression ratio and file size.
- -t: Test the integrity of a ZIP file without extracting it.
- -u: Update an existing ZIP file by adding new or modified files.
- -m: Move the original files to the ZIP file after compression, instead of copying them.
- -n: Do not compress the files that have the specified extensions, such as .jpg or .mp3.
- -q: Suppress the output messages, except for errors and warnings.
- -o: Overwrite the existing files without prompting when extracting.
- -x: Exclude the files that match the specified pattern when zipping or unzipping.
For more information and examples, you can check the manual pages of zip and unzip by typing man zip and man unzip in the terminal.
Conclusion
In this article, you learned how to create and extract ZIP files from the Linux terminal with the zip and unzip commands. You also learned some of the most common and useful options that can help you customize and optimize your ZIP operations. ZIP files are a great way to compress, store, and share your data across different platforms and applications.