The Linux Unzip Command

man using laptop
Paul Bradbury/Caiaimage/Getty Images

Zipping files is an easy, efficient way to transfer them between computers and servers using far less bandwidth than sending full-size files. When you receive a zipped archive in Linux, decompressing it is just as easy. Here are a few ways to use the unzip command in the Linux command line.

Decompressing a Single Zip File Into the Current Folder

The basic syntax for decompressing a file is:

unzip filename

As an example, say you've zipped up an album called "Menace To Sobriety" by the band Ugly Kid Joe as a zip file named "Menace To Sobriety."

To unzip this file into the current folder, simply run the following command:

unzip "Menace To Sobriety"

Decompressing Multiple Files

The man command lets you decompress more than one file at a time using the following syntax:

unzip filename1 filename2 filename3

Say you zipped up three files of Alice Cooper albums named "Trash," "Hey Stoopid," and "Dragontown" separately. To unzip these files, you might try entering the following:

unzip "Trash.zip" "Dragontown.zip" "Hey Stoopid.zip"

What you then get, however, is this error:

Archive: Trash.zip caution: filename not matched: Dragontown.zip

Assuming the three files live in the same folder, a better way is to use the following command instead:

unzip '*.zip'

Be careful, though: This command is indiscriminate and will decompress every zip file in the current folder.

Unzip a File but Exclude Certain Others

If you have a zip file and you want to extract all the files except for one, use the -x switch, as follows:

unzip filename.zip -x filetoexclude.zip

To continue with our example, the album "Trash" by Alice Cooper has a song titled "Bed Of Nails." To extract all the songs except for "Bed Of Nails," you'd use the following syntax:

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, like this:

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

To see all the songs in the album "Trash.zip," use the following:

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 OK 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 will be 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... "

Show Detailed Information About a Compressed File

If you would like more detailed information, use the -v switch, which outputs more verbose information:

The syntax is as follows:

unzip -v filename

For example:

unzip -v Trash.zip

The verbose output contains the following information:

  • Length in bytes
  • Method
  • Size
  • Compression percentage
  • Date and time created
  • CRC
  • Name

Decompress a Zip File to the Current Directory Without Creating Directories

If you added folders within a zip file while creating it, then the standard unzip command will recreate the folder structure as it is unzipped.

For example, if you extract a zip file called "filename1.zip" with the following structure, the folders will be recreated when you unzip it:

  • Folder 1: filea.txt, fileb.txt, filec.txt
  • Folder 2: filed.txt, filee.txt
  • Folder 3: filef.txt

If you want all the ".txt" files to extract into the current folder without the folders being recreated, you'd use the -j switch, as follows:

unzip -j filename.zip

Decompress a File Without Prompting When Files Already Exist

Imagine you have a zip file that you've already unzipped, and you've started working on the files that you have extracted.

If you have another file you want to unzip and the zip file contains files that already exist in the target folder, a warning is displayed before the system overwrites the files. This is OK, but if you are extracting a file with 1000 files in it, you don't want to be prompted every time.

So, if you don't want to overwrite existing files, use the -n switch:

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

Extracting 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

Unzipping a File Without Displaying Any Output

By default, the "unzip" command lists everything it is doing, including listing every file in the archive as it is extracting it. 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.

Linux provides dozens of other switches. Visit the Linux man pages to learn more.