Unzipping archives from the command line

For many years, the de facto standard in file archives was Pkzip and Pkunzip, by Phil Katz. Phil created the Zip archive file format because he had previously written a program to work with Arc archive files, but ran into legal issues. As a result, when Phil created Pkzip and Pkunzip, he also took an extra step: he made the Zip file format an open standard so anyone could work with it.

That helped make Zip an extremely popular archive format, and throughout the 1990s, Zip archives were extremely common. Zip remains commonplace today. And you can find many things shared as Zip files, including downloading files from GitHub or Google Docs.

Your graphical desktop almost certainly can manage Zip files on its own. Extracting a Zip archive from your file manager should be as easy as right-clicking on the file and selecting “Extract here” from the pop-up menu. But it’s just as easy to extract or “unzip” these files from the command line.

Listing Zip files

Info-Zip is a collection of command line programs to create, extract, and manipulate Zip archives. Every Linux distribution that I know of already includes the Info-Zip programs by default, so you should be able to use these commands without installing anything.

Let’s start by looking at the contents of a Zip file. Because Zip files can contain paths to files, in addition to just files, it can be helpful to know where files will be extracted to before you “unzip” an archive with the unzip command. Will the extracted files go into their own subdirectory, into the current directory, or is there a mix of where they’ll go?

Use the unzip command with the -l option to list the contents of the Zip archive. In this case, I’m working with the FreeDOS monthly test release, which is made available as CD-ROM and DVD “ISO” images, compressed in a Zip file:

$ ls -l
total 714468
-rw-r--r--. 1 jhall jhall 435524029 Oct  1 05:46 FDT2510-BonusCD.zip
-rw-r--r--. 1 jhall jhall 296087315 Oct  1 05:46 FDT2510-LiveCD.zip

$ unzip -l FDT2510-LiveCD.zip
Archive:  FDT2510-LiveCD.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     5963  09-30-2025 23:00   readme.txt
  1474560  09-30-2025 23:00   T2510BOOT.img
318941184  09-30-2025 23:00   T2510LIVE.iso
---------                     -------
320421707                     3 files

$ unzip -l FDT2510-BonusCD.zip
Archive:  FDT2510-BonusCD.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     5963  09-30-2025 23:00   readme.txt
445032448  09-30-2025 23:00   T2510BNS.iso
---------                     -------
445038411                     2 files

Here, I can see that the FDT2510-LiveCD.zip archive contains three files: a Readme, a virtual machine “IMG” virtual disk image, and a CD-ROM/DVD “ISO” image file. The ls command showed that the archive is 296,087,315 bytes in size (about 282 Mb) and but the total size of the contents is 320,421,707 bytes (about 305 MB), so we know we’d need at least that much free space on our disk to extract it.

We can also do a little math to see that the Zip archive is able to perform some compression. The Zip file is 282 MB but its contents are 305 MB, saving 24,334,392 bytes (about 23 MB). Most of the LiveCD is a bunch of Zip files which are already compressed, plus a “Live” FreeDOS environment, so it’s not too surprising that the LiveCD doesn’t compress down much. Zip files do much better with text files.

Similarly, I can see that the FDT2510-BonusCD.zip archive has just two files: a Readme and an “ISO” file. The Zip file is 435,524,029 bytes (about 415 MB) and the contents are 445,038,411 bytes (about 424 MB), saving about 9 MB. The BonusCD is almost entirely Zip files, so there’s not much space to save.

Extracting Zip files

To get the files out of the Zip file, you use the unzip command without parameters, just the filename. This will extract everything in the Zip file using any paths that are listed in the Zip file. In this case, these Zip files do not contain paths to files, so the unzip command will extract them into the current directory:

$ unzip FDT2510-LiveCD.zip
Archive:  FDT2510-LiveCD.zip
  inflating: readme.txt
  inflating: T2510BOOT.img
  inflating: T2510LIVE.iso

$ ls
FDT2510-BonusCD.zip  readme.txt     T2510LIVE.iso
FDT2510-LiveCD.zip   T2510BOOT.img

Both Zip archives have a file called readme.txt in them. When I unzip the BonusCD Zip file, the unzip command will ask me what to do with the second readme.txt file:

$ unzip FDT2510-BonusCD.zip
Archive:  FDT2510-BonusCD.zip
replace readme.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: readme.txt
  inflating: T2510BNS.iso

$ ls
FDT2510-BonusCD.zip  readme.txt    T2510BOOT.img
FDT2510-LiveCD.zip   T2510BNS.iso  T2510LIVE.iso

There is an option to unzip to overwrite files by default, but I don’t like to overwrite files automatically. If you want to learn more about this, see the “man” page.

Extracting specific contents

If you just want to extract specific files from a Zip archive, you can specify them on the unzip command line. For example, let’s start with just the two Zip files:

$ ls
FDT2510-BonusCD.zip  FDT2510-LiveCD.zip

Let’s say I only want to extract the “ISO” files from each Zip file. This is actually a common thing that I do with the FreeDOS monthly test releases, so I can install a fresh copy of the “test” distribution. To list the contents of each Zip file, I use unzip with the -l option. If I only want to list the “ISO” files in each archive, I can specify *.iso as part of the unzip command line:

$ for f in *.zip; do unzip -l $f '*.iso'; done
Archive:  FDT2510-BonusCD.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
445032448  09-30-2025 23:00   T2510BNS.iso
---------                     -------
445032448                     1 file
Archive:  FDT2510-LiveCD.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
318941184  09-30-2025 23:00   T2510LIVE.iso
---------                     -------
318941184                     1 file

To extract just the “ISO” files from each Zip archive, I can use unzip again without any command line options, and specifying just *.iso as what files I want to list from the Zip archive:

$ for f in *.zip; do unzip $f '*.iso'; done
Archive:  FDT2510-BonusCD.zip
  inflating: T2510BNS.iso            
Archive:  FDT2510-LiveCD.zip

Unzipping at the command line

While I love that my graphical desktop system supports Zip files by default, extracting them via the file manager is an “all or nothing” affair. To get more control over what files I want to extract from the Zip file, it’s helpful to know a few commands that you can run from the shell. The unzip and zip commands are excellent tools to add to your “toolbelt” when using the command line. Learn more about them from their “man” pages: zip(1) and unzip(1).

Leave a Reply