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.
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
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
Unzip command in Linux
Let’s now shift gears and focus on unzip command. The command is used to unzip or decompress zipped files and directories. Let’s take a look at a few example usages of the command:
1. See the content of the zip file without extracting
If you want to see what the zip file contains, you son’t always have to extract it first. You can use the -l option
and it will show the content of the zip file.
$ unzip -l archive.zip
As you can see, it also shows the timestamp of the files and the actual size of the individual files in bytes.
$ unzip -l archive.zip Archive: archive.zip Length Date Time Name --------- ---------- ----- ---- 0 2020-08-21 20:22 file1.txt 0 2020-08-21 20:22 file2.txt 0 2020-08-21 20:22 file3.txt 0 2020-08-21 20:22 file4.txt --------- ------- 0 4 files
To view more detailed information about the file such as file permissions and the total size of the files in the archive, use the –
Z option
as shown:
$ unzip -Z archive.zip Archive: archive.zip Zip file size: 606 bytes, number of entries: 4 -rw-rw-r-- 3.0 unx 0 bx stor 20-Aug-21 20:22 file1.txt -rw-rw-r-- 3.0 unx 0 bx stor 20-Aug-21 20:22 file2.txt -rw-rw-r-- 3.0 unx 0 bx stor 20-Aug-21 20:22 file3.txt -rw-rw-r-- 3.0 unx 0 bx stor 20-Aug-21 20:22 file4.txt 4 files, 0 bytes uncompressed, 0 bytes compressed: 0.0%
2. How to unzip/decompress a zipped file
In its basic and simplest syntax, unzipping a zipped file takes the syntax below and occurs in the current working directory.
$ unzip archive.zip
Example output:
$ unzip archive.zip Archive: archive.zip extracting: file1.txt extracting: file2.txt extracting: file3.txt extracting: file4.txt
3. Unzip a file to another directory
To extract or unzip an archive to a different destination, use the -d flag and specify the destination path as shown
$ unzip archive.zip -d /path/to/destination/folder
4. Overwrite all the files without prompting
If there are already files with the same name in the directory where you are extracting the files, you’ll be promoted for each such files. You can force overwrite all the files with option -o
.
$ unzip -o archive.zip
5. Do not overwrite any files
If you don’t want any existing file to be overwritten by newly extracted files, use the -n option
(stands for never overwrite).
$ unzip -n archive.zip
6. Exclude certain files from being decompressed
Just as you can extract a specific file(s) from an archive, so can you exclude certain files from being decompressed. This is possible using the -x option
in the syntax shown:
$ unzip archive.zip -x file1.txt file2.txt
7. How to suppress the output of unzip command
As you may have noted, details of the unzip operation showing the location of the extraction and files being extracted is printed on the terminal. To suppress this info, use the -q option
as shown:
$ unzip -q archive.zip
8. Overwrite all the files without prompting
If there are already files with the same name in the directory where you are extracting the files, you’ll be promoted for each such files. You can force overwrite all the files with option -o
.
$ unzip -o archive.zip
9. Unzip a Password Protected ZIP file
To unzip a file that is password-protected, invoke the unzip command with the -P option
followed by the password:
$ unzip -P PasswOrd archive.zip
Typing a password on the command line is insecure and should be avoided. A more secure option is to extract the file normally without providing the password. If the ZIP file is encrypted, unzip will prompt you to enter the password:
unzip archive.zip archive: archive.zip [archive.zip] file1.txt password:
unzip will use the same password for all encripted files as long as it is correct.
10. Unzip Multiple ZIP Files
You can use regular expressions to match multiple archives.
For example, if you have multiple ZIP files in your current working directory you can unzip all files using only one command:
$ unzip '*.zip'
Note the single quotes around the *.zip
. If you forgot to quote the argument, the shell will expand the wildcard character, and you will get an error.
Conclusion
In this tutorial, we have understood the compression(zip) and decompression(unzip) concept and we have seen how we can manipulate compressed files. Let us know if you know about more options.