The Linux ‘unzip’ Command
Zipping files is an easy, efficient way to transfer data between computers and servers. When files are compressed, they not only save disk space on a local drivebut also make it easier and more convenient to download files from the internet, using far less bandwidth than sending full-size files.
When you receive a zipped archive in Linux, decompressing it is just as easy. There are lots of switches available in Linux, which means that you have many ways to extract files with the unzip command in the command line.
Decompress Single ZIP Files
The basic syntax for decompressing a file is:
unzip filename
As an example, say you’ve zipped up an album named Menace to Sobriety. To unzip this file to the current folder, you’d simply run the following command:
unzip "Menace To Sobriety"
Decompress Multiple ZIP Files
The man command lets you decompress more than one file at a time using the following syntax:
unzip filename1 filename2 filename3
If you’ve zipped up three files of Alice Cooper albums named Trash, Hey Stoopid, and Dragontown, separately, you might try this to unzip them:
unzip "Trash.zip" "Dragontown.zip" "Hey Stoopid.zip"
However, what you’d get is this error:
Archive: Trash.zip caution: filename not matched: Dragontown.zip
Assuming the three files live in the same folder, a better method is to use the following command:
unzip '*.zip'
Be careful, though. This command is indiscriminate and will decompress every ZIP file in the current folder.
Exclude Some ZIP Files
If you have a ZIP file and you want to extract all the files except for one, use the -xswitch.
unzip filename.zip -x filetoexclude.zip
To continue with our example, the album “Trash” in Trash.zip has an MP3 titled Bed Of Nails. To extract all the songs except for “Bed Of Nails,” you’d do this:
unzip Trash.zip -x "Bed Of Nails.mp3"
Extract a ZIP File to a Different Directory
If you want to put the contents of the ZIP file in a different directory than the current one, use the -d switch.
unzip filename.zip -d path/to/extract/to
For example, to decompress the Trash.zip file to /home/music/Alice Cooper/Trash, you’d use the following syntax:
unzip Trash.zip -d "/home/music/Alice Cooper/Trash"
How to Show the Contents of a Compressed Zip File
To list the contents of a compressed file, use the -l switch.
unzip -l filename.zip
In our example, we could use this switch to see all the files in Trash.zip.
unzip -l Trash.zip
The information returned includes:
- Length in bytes
- Date created
- Time created
- Name
How to Test If a ZIP File Is Valid
To test whether a ZIP file is structured correctly and can be used properly before extracting it, use the -t switch.
unzip -t filename.zip
For example, to test whether Trash.zip is valid, you could run the following:
unzip -t Trash.zip
Each file is listed, and OK should appear next to it. At the bottom of the output, a message should appear stating no errors detected in compressed data of….
See Detailed Information on a ZIP File
The -v switch (verbose) can give more detailed information.
unzip -v filename
To use this switch with Trash.zip to see more information, we’d type:
unzip -v Trash.zip
The output contains the following information:
- Length in bytes
- Method
- Size
- Compression percentage
- Date and time created
- CRC
- Name
Decompress a ZIP File Without Making Directories
For ZIP files that have folders, executing unzip alone, without switches, would re-create the same folder structure from the archive.
Extracting filename1.zip, for example, which has the following three folders, would result in the same folders being extracted:
- Folder 1: filea.txt, fileb.txt, filec.txt
- Folder 2: filed.txt, filee.txt
- Folder 3: filef.txt
In this example, to extract all of the TXT files to the current folder without making those three folders, just append -j to the end of the command.
unzip -j filename1.zip
Decompress a ZIP File Without Prompting to Overwrite
Suppose you’ve already unzipped a particular ZIP file and have started working on the unzipped files, changing and updating them however you wish. The last thing you want is to have those files overwritten when you extract a ZIP that has files with those same names. You’d immediately lose everything you were working on when the new files replace your existing ones.
However, you can use the -n switch if you want to not overwrite existing files. Every file from the ZIP archive that has a name matching a file in the extracted folder will not overwrite anything when this switch is used. Everything else, however, that has a unique name will still be extracted.
unzip -n filename.zip
If you don’t care whether the file already exists and you always want to overwrite the files as they are extracted without prompting, use the -o switch.
unzip -o filename.zip
Extract Password-Protected ZIP Files
If you need to unzip a file that requires a password for access, use the -p switch followed by the password.
unzip -P password filename.zip
For example, to unzip a file called cats.zip with the password kittens123, use the following:
unzip -P kittens123 filename.zip
Unzip a File Without Displaying Any Output
By default, the unzip command lists everything it’s doing, including showing every file in the archive as the command is extracting them. You can suppress this output by using the -q switch.
unzip -q filename.zip
This unzips the filename without providing any output and returns you to the cursor when it has finished.