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
[ads]
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
[ads]
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:
[ads]
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.