Zip is a command-line utility tool used for compressing files and folders. Compression of files & folders enables faster and more efficient transfer, storage, and emailing of files and folders. On the other hand, unzip is a utility tool that helps you decompress files and folders.
In this tutorial, we will show you how to Zip (compress) and Unzip (decompress) files and directories in Linux using the zip and unzip command.
Install Zip/Unzip in Ubuntu/Debian/Linux Mint
Open the terminal and run the following apt command,
$ sudo apt install -y zip unzip or $ sudo apt-get install -y zip unzip
After installation, you can confirm the version of zip
and unzip
installed using the command.
$ zip -v and $ unzip -v
How to Install Zip/Unzip in RedHa/CentOS/Fedora
Open the terminal and execute the beneath command,
$ sudo yum install -y zip unzip or $ sudo dnf install -y zip unzip
Let’s dive in and see how to zip and unzip files and directories in Linux with zip and unzip command with examples.
[ads]
Zip command in Linux
Following is the syntax for using the zip command once its properly installed.
$ zip [options...] name-of-file.extension [FILES...]
This is the basic syntax of using the zip command to create a zip archive of a file. We can provide different options for the zip command along with a name for the zip archive that will be created followed by the files that you want to archive. You can archive a single file, multiple files or a complete directory using the zip command.
1. Zipping a single file and multiple files with zip command
Let’s assume you have a text file – file1.txt
– in your current directory and you want to zip it into an archive called archive.zip
.
The command for this operation will be:
$ zip archive.zip file1.txt
Additionally, you can zip multiple files at a go into an archive as shown:
$ zip archive.zip file1.txt file2.txt file3.txt
2. Adding a file to a zip archive (-u)
At times, you may find the need to add a file to a zip archive. To do so, use the -u
flag. For example, to add another file file4.txt, run:
$ zip -u archive.zip file4.txt
3. View contents of a zipped file
To view contents of a zipped file, use the command as shown:
$ zipinfo archive.zip
4. Zipping all the files in the current directory
If you have multiple files in your current directory, you can zip all of them at a go using the wildcard symbol as shown in the syntax below:
$ zip archive.zip *
For example, to compress all files in the home directory to home.zip
archive, execute the command below. Be sure that you are working in the home directory.
$ zip home.zip *
5. Delete a file from an archive (-d)
To remove a file from an archive, invoke the -d
flag. For instance, to remove file4.txt from the zipped file, run:
$ zip -d archive.zip file4.txt
6. Delete files after zipping (-m)
As you may have noted, the original files remain even after zipping or archiving them. If you prefer you get rid of them during archival and conserve space, invoke the -m option
during archival as shown:
$ zip -m archive.zip file1.txt file2.txt file3.txt file4.txt
[ads]
7. How to zip a directory (-r)
We have so far seen how to zip files. Often, you will be tasked with zipping directories since they take up more space for the most time. To zip a folder, use the syntax below. The -r option
zipped the folder recursively.
$ zip -r archive.zip folder
As with files, you can also zip several folders concurrently as shown:
$ zip -r archive.zip folder1 folder2 folder3
8. Zip a file to a different destination
To zip a file to a different destination other than the current directory simply specify the path to the zipped archive in your syntax as shown:
$ zip /path/to/destination/archive.zip file
9. Creating a Password Protected ZIP file
If you have sensitive information that needs to be stored in the archive you can encrypt it using the -e option
:
$ zip -e archive.zip directory
You will be prompted to enter and verify the archive password:
Enter password: Verify password:
10. Creating Split Zip File
Imagine you want to store the Zip archive on a file hosting service that has a file size upload limit of 1GB and your Zip archive is 5GB.
You can create a new split Zip file using the -s option followed by specified size. The multiplier can be k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes).
$ zip -s 1g -r archive.zip directory
The command above will keep creating new archives in a set after it reaches the specified size limit.
Example output:
archive.zip archive.z01 archive.z02 archive.z03 archive.z04