Table of Contents
- Install Zip and Unzip
- Zip a File with zip Command
- Add a File to an existing Zip Archive
- View the Content of the Zip File
- Remove a File From an Existing Zip Archive
- Delete Original Files After Zipping
- Zip a Directory with zip Command
- Create a Password Protected Zip File
- Unzip a Zipped File
- View the Content of the Zipped File
- Split a Zip Archives into Multiple Files
- Conclusion
Compressing and uncompressing files and directories is a common task for any system administrator. It is an essential skill when you are working on a headless remote server. Many tools are available to compress and uncompress files and directories. Zip is a popular and cross-platform command-line tool used to compress and archive data in Linux. It allows you to combine multiple files and directories into a single archive file. The Unzip command is used to decompress or extract the content from the compressed archive.
This post will show you how to use the Zip and Unzip commands in Linux.
Install Zip and Unzip
By default, the Zip and Unzip tool is available in the default repository of all major Linux distributions.
For Debian and Ubuntu operating systems, install Zip and Unzip with the following command:
apt-get install zip unzip -y
For RHEL, Rocky Linux, and Fedora operating systems, install Zip and Unzip with the following command:
dnf install zip unzip -y
Also Read
Zip a File with zip Command
The basic syntax to zip a file is shown below:
zip [OPTION] file.zip file
For example, to zip a single file run the following command:
zip file1.zip file1.txt
To zip multiple files, run the following command:
zip files.zip file1.txt file2.txt file3.txt
Output:
adding: file1.txt (deflated 58%) adding: file2.txt (deflated 58%) adding: file3.txt (deflated 58%)
Add a File to an existing Zip Archive
You can use the zip command with -u option to add an additional file to an existing zip archive.
zip -u files.zip file4.txt
Output:
adding: file4.txt (deflated 58%)
View the Content of the Zip File
You can use the zipinfo command to view the content of the zip file.
For example, to view the content of the files.zip file, run the following command:
zipinfo files.zip
Output:
Archive: files.zip Zip file size: 12634 bytes, number of entries: 4 -rw-r--r-- 3.0 unx 7224 tx defN 22-Apr-19 17:16 file1.txt -rw-r--r-- 3.0 unx 7224 tx defN 22-Apr-19 17:16 file2.txt -rw-r--r-- 3.0 unx 7224 tx defN 22-Apr-19 17:16 file3.txt -rw-r--r-- 3.0 unx 7224 tx defN 22-Apr-19 17:20 file4.txt 4 files, 28896 bytes uncompressed, 12028 bytes compressed: 58.4%
Remove a File From an Existing Zip Archive
You can use the zip command with -d option to remove a file from the zip archive.
For example, to remove a file2.txt from the files.zip archive, run the following command:
zip -d files.zip file2.txt
Output:
deleting: file2.txt
Delete Original Files After Zipping
When you compress single or multiple files using the zip command, zip keeps the original and compressed files. After creating the zipped archive, you can use the -m option to delete the original files.
zip -m newfiles.zip file1.txt file2.txt file3.txt file4.txt
Output:
adding: file1.txt (deflated 58%) adding: file2.txt (deflated 58%) adding: file3.txt (deflated 58%) adding: file4.txt (deflated 58%)
Zip a Directory with zip Command
Zip also allows us to compress the directory using the zip command. You will need to use the -r option to zip the directory recursively.
The basic syntax to zip a directory is shown below:
zip -r directory.zip directory
For example, to zip a directory named students, run the following command:
zip -r students.zip students
Output:
adding: students/ (stored 0%) adding: students/gujarati.txt (deflated 58%) adding: students/maths.txt (deflated 58%) adding: students/english.txt (deflated 58%)
To zip multiple directories, run the following command:
zip -r directory.zip directory1 directory2 directory3
Also Read
How to Check Size of Files and Directory on Linux
Create a Password Protected Zip File
Zip also allows us to create a password-protected zip archive. You can create a password-protected zip file using the -e option with the zip command.
zip -r -e teachers.zip teachers
Output:
Enter password: Verify password: adding: teachers/ (stored 0%) adding: teachers/gujarati.txt (deflated 58%) adding: teachers/maths.txt (deflated 58%) adding: teachers/english.txt (deflated 58%)
Unzip a Zipped File
The basic syntax to unzip a file is shown below:
unzip file.zip
For example, to unzip a zipped file named files.zip, run the following command:
unzip files.zip
Output:
Archive: files.zip inflating: file1.txt inflating: file3.txt inflating: file4.txt
If you want to unzip a file to a different directory, use the -d option with the unzip command.
For example, to unzip a file named students.zip to a students directory, run the following command:
unzip students.zip -d students
Output:
Archive: students.zip creating: students/students/ inflating: students/students/gujarati.txt inflating: students/students/maths.txt inflating: students/students/english.txt
To extract a single file named file1.txt from the files.zip file, run the following command:
unzip files.zip file1.txt
Output:
Archive: files.zip inflating: file1.txt
If you want to overwrite the existing files without being prompted, use the -o option as shown:
unzip -o files.zip
View the Content of the Zipped File
You can use the -l option with the unzip command to view the content of a zipped file.
For example, to list the content of a zipped file named students.zip, run the following command:
unzip -l students.zip
Output:
Archive: students.zip Length Date Time Name --------- ---------- ----- ---- 0 2022-04-19 17:17 students/ 7224 2022-04-19 17:17 students/gujarati.txt 7224 2022-04-19 17:17 students/maths.txt 7224 2022-04-19 17:17 students/english.txt --------- ------- 21672 4 files
To list the content of a zipped file with detailed information about the file, use the -Z option:
unzip -Z students.zip
Output:
Archive: students.zip Zip file size: 9691 bytes, number of entries: 4 drwxrwxr-x 3.0 unx 0 bx stor 22-Apr-19 17:17 students/ -rw-r--r-- 3.0 unx 7224 tx defN 22-Apr-19 17:17 students/gujarati.txt -rw-r--r-- 3.0 unx 7224 tx defN 22-Apr-19 17:17 students/maths.txt -rw-r--r-- 3.0 unx 7224 tx defN 22-Apr-19 17:17 students/english.txt 4 files, 21672 bytes uncompressed, 9021 bytes compressed: 58.4%
Split a Zip Archives into Multiple Files
Sometimes, the size of a zipped file is very large, and you can not send it as an attachment via email. In this case, you can use the zip command to break up a zipped archive into multiple smaller files.
For example, to create a zipped archive of the directory named directory into a 3MB file, run the following command:
zip -r -s 3m file.zip directory
You can now verify all created zipped files using the following command:
ls
Output:
directory file.z01 file.z02 file.z03 file.z04 file.z05 file.z06 file.z07 file.z08 file.z09 file.z10 file.zip
You can also check the size of each file with the following command:
du -hs *
You should see the following output:
3.0M file.z01 3.0M file.z02 3.0M file.z03 3.0M file.z04 3.0M file.z05 3.0M file.z06 3.0M file.z07 3.0M file.z08 3.0M file.z09 3.0M file.z10 1.7M file.zip
Conclusion
In this guide, we explained how to use the Zip and Unzip commands to compress and uncompress files and directories in Linux. I have demonstrated both commands’ usage with real-life examples for better understanding. Hopefully, you can now easily use the Zip and Unzip commands in your day-to-day operations. Try it on dedicated server hosting from Atlantic.Net!