How to Zip a Folder in Linux

By | 2019-09-11

Zipping a Folder

Use the zip command with the -r option to zip a folder on a Linux system.

For example, to zip a folder named bigfiles, you would use the following command:

$ zip -r bigfiles.zip bigfiles
  adding: bigfiles/ (stored 0%)
  adding: bigfiles/bigfile2 (deflated 98%)
  adding: bigfiles/bigfile3 (deflated 96%)
  adding: bigfiles/bigfile1 (deflated 97%)

After the -r, you specify the name of the zip file you wish to create, followed by a list of files or folders you wish to be in the zip file. The deflated percentages next to each file name indicate how much zip was able to reduce the disk usage of that file. In this case, bigfiles/bigfile2 takes up 98% less space in the zip file than the original. The amount of deflation you can get depends on the type of data you are attempting to compress.

The -r option is short for recursive. I.e. don’t just zip the folder, but the contents of the folder as well. If you Omit -r, the resulting zip file will have an empty folder in it.

$ zip bigfiles.zip bigfiles
  adding: bigfiles/ (stored 0%)

Notice how zip didn’t list the contents of bigfiles when -r was left out.

Listing a Zip File’s Contents

It is generally a good idea to list the contents of a zip file before unzipping it. Sometimes the zip file won’t have the contents in a folder, so unzipping it will make a mess of the folder you are currently in. You may also wish to see how much space will be used by unzipping the file.

To list a zip file’s contents use unzip -l followed by the name of the zip file. E.g:

$ unzip -l bigfiles.zip
Archive:  bigfiles.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2019-09-09 12:27   bigfiles/
     6292  2019-09-09 12:28   bigfiles/bigfile2
     1859  2019-09-09 12:27   bigfiles/bigfile3
     3432  2019-09-09 12:28   bigfiles/bigfile1
---------                     -------
    11583                     4 files

In the example 11583 is the amount of space in bytes you would use if you were to extract the file.

Unzipping a Zip File

To unzip a zip file, use the unzip command followed by the name of the zip file.

$ unzip bigfiles.zip 
Archive:  bigfiles.zip
   creating: bigfiles/
  inflating: bigfiles/bigfile2 
  inflating: bigfiles/bigfile3 
  inflating: bigfiles/bigfile1

As you can see, unzip lists the files and folders created when extracting the zip file.

Tips

Both the zip and unzip commands have a help option that show a summary of their options:

$ zip -h
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help
$ unzip -h
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

  -p  extract files to pipe, no messages     -l  list files (short format)
  -f  freshen existing files, create none    -t  test compressed archive data
  -u  update files, create if necessary      -z  display archive comment only
  -v  list verbosely/show version info       -T  timestamp archive to latest
  -x  exclude files that follow (in xlist)   -d  extract files into exdir
modifiers:
  -n  never overwrite existing files         -q  quiet mode (-qq => quieter)
  -o  overwrite files WITHOUT prompting      -a  auto-convert any text files
  -j  junk paths (do not make directories)   -aa treat ALL files as text
  -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields
  -C  match filenames case-insensitively     -L  make (some) names lowercase
  -X  restore UID/GID info                   -V  retain VMS version numbers
  -K  keep setuid/setgid/tacky permissions   -M  pipe through "more" pager
See "unzip -hh" or unzip.txt for more help.  Examples:
  unzip data1 -x joe   => extract all files except joe from zipfile data1.zip
  unzip -p foo | more  => send contents of foo.zip via pipe into program more
  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer

If a zip file's contents aren't contained in a folder, create a new directory and change to it when extracting a zip file. This will prevent you from inadvertently cluttering up a folder.

$ unzip -l zipfile.zip 
Archive:  zipfile.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      322  2019-09-10 15:15   file1
      322  2019-09-10 15:15   file2
      322  2019-09-10 15:15   file3
---------                     -------
      966                     3 files
$ mkdir zip_contents
$ cd zip_contents
$ unzip ../zipfile.zip
Archive:  ../zipfile.zip
  inflating: file1                   
  inflating: file2                   
  inflating: file3

References