How to zip and unzip from the command line

Image from Roadsidepictures – CC BY-NC-ND 2.0
Image by Roadsidepictures – CC BY-NC-ND 2.0

There are lots of GUI utilities to zip and unzip files but sometimes you only have access to the command line on a machine.  Linux has different types of data compression  formats (.tar, tar.gz, tar.bz2, etc.) but I find the zip utilities easier to use and more compatible with Windows systems. So how do you zip and unzip files from the command line?

First install the zip and unzip packages. If you are on a Debian-based system like Ubuntu:

sudo apt-get install -y zip unzip

or if you are on Red Hat/CentOs:

sudo yum install -y zip unzip

The most intuitive way to zip a directory and all its files  is to navigate to that folder and zip everything recursively inside the current directory:

zip -r foo.zip .

To extract the zip file into a given directory (e.g.  /tmp), navigate to the directory where the zip file is stored and unzip it with:

unzip foo.zip -d /tmp

Leave a Reply

Your email address will not be published. Required fields are marked *