Howto Extract Zip Files in a Linux and Unix-like Systems

I‘m a new Linux system user. I’ve downloaded a file named from internal ftp server. How can I extract a zip file on a Linux or Unix based system using a command line options?

You need to use the unzip command on a Linux or Unix like system to exract (unzip) files. This command may not be installed by default on a Linux operating systems.


Install unzip command

First, open the terminal application. Next, use any one of the following command as per your Linux or Unix variant to install the unzip tool:

Debian/Ubuntu Linux user install unzip

The syntax is as follows using apt-get command:

sudo apt-get install unzip

CentOS/RHEL/Fedora Linux user install unzip

The syntax is as follows using yum command:

sudo yum install unzip

How do I use unzip command on a Linux or Unix like systems?

The syntax is:

unzip zipfilename
unzip zipfilename fileToExtract
unzip [optionz] zipfilename

Extract a zip file named

To use unzip to extract all members of the archive into the current directory and subdirectories below it, creating any subdirectories as necessary, type:
unzip database
Another example:

unzip foo
<pre lang="bash">

Sample outputs:

   creating: etc/
  inflating: etc/afpovertcp.cfg      
  inflating: etc/aliases             
   creating: etc/apache2/
   creating: etc/apache2/extra/
  inflating: etc/apache2/extra/httpd-autoindex.conf  
  inflating: etc/apache2/extra/httpd-dav.conf  
  inflating: etc/apache2/extra/httpd-default.conf  
  inflating: etc/apache2/extra/httpd-info.conf  
  inflating: etc/apache2/extra/httpd-languages.conf  
  inflating: etc/apache2/extra/httpd-manual.conf  
  inflating: etc/apache2/extra/httpd-mpm.conf  
  inflating: etc/sshd_config         
  inflating: etc/sshd_config~previous  
  inflating: etc/syslog.conf         
  inflating: etc/ttys                
 extracting: etc/xtab                
  inflating: etc/zshenv       

To extract all python (*.py) files, enter:
unzip "*.py"
To extract all C source files “*.c”, and “*.h” and a Makefile into the ~/projects/editor directory:

unzip "*.[ch]" Makefile -d ~/projects/editor

Extract all files except bar.txt from zipfile

 unzip -x bar.txt

Extract files into given directory

To extract files into /tmp/data instead of the default current directory, type:

unzip -d /tmp/data
unzip -d /tmp/data pic1.jpg

Sample outputs:

   creating: ../ddd/etc/
  inflating: ../ddd/etc/afpovertcp.cfg  
  inflating: ../ddd/etc/aliases      
   creating: ../ddd/etc/apache2/
   creating: ../ddd/etc/apache2/extra/
  inflating: ../ddd/etc/apache2/extra/httpd-autoindex.conf  
  inflating: ../ddd/etc/apache2/extra/httpd-dav.conf  
  inflating: ../ddd/etc/apache2/extra/httpd-default.conf  

Forcefully overwrite existing files when unzip

unzip -o
unzip -o data.jpg

The -o option will overwrite files WITHOUT prompting. Pass the -n option to never overwrite existing files:

unzip -n
unzip -n data.jpg

List files stored in a zip file on screen

unzip -l
unzip -l | more
unzip -l

Sample outputs:

  Length     Date   Time    Name
 --------    ----   ----    ----
        0  04-24-15 13:08   etc/
      515  09-10-14 02:17   etc/afpovertcp.cfg
     9970  09-10-14 04:06   etc/aliases
        0  11-06-14 06:51   etc/apache2/
        0  09-10-14 03:39   etc/apache2/extra/
     2877  09-10-14 03:39   etc/apache2/extra/httpd-autoindex.conf
     1747  09-10-14 03:39   etc/apache2/extra/httpd-dav.conf
     2942  09-10-14 03:39   etc/apache2/extra/httpd-default.conf
     1119  09-10-14 03:39   etc/apache2/extra/httpd-info.conf
     5078  09-10-14 03:39   etc/apache2/extra/httpd-languages.conf
     1049  09-10-14 03:39   etc/apache2/extra/httpd-manual.conf
     4456  09-10-14 03:39   etc/apache2/extra/httpd-mpm.conf
     2216  09-10-14 03:39   etc/apache2/extra/httpd-multilang-errordoc.conf
    11406  09-10-14 03:39   etc/apache2/extra/httpd-ssl.conf
      607  09-10-14 03:39   etc/apache2/extra/httpd-userdir.conf
     1519  09-10-14 03:39   etc/apache2/extra/httpd-vhosts.conf
     3161  09-10-14 03:39   etc/apache2/extra/proxy-html.conf
    20786  09-10-14 03:41   etc/apache2/httpd.conf
      382  06-25-14 16:46   etc/
     4161  02-03-15 00:47   etc/sshd_config
     4161  05-05-14 18:23   etc/sshd_config~previous
       96  09-10-14 02:17   etc/syslog.conf
     1316  09-10-14 02:17   etc/ttys
        0  09-10-14 02:17   etc/xtab
      126  09-10-14 04:48   etc/zshenv
 --------                   -------
  2747844                   281 files

Test or verify compressed archive data

unzip -t foo
unzip -t

Sample outputs:

    testing: etc/                     OK
    testing: etc/afpovertcp.cfg       OK
    testing: etc/aliases              OK
    testing: etc/apache2/             OK
    testing: etc/apache2/extra/       OK
    testing: etc/apache2/extra/httpd-autoindex.conf   OK
    testing: etc/apache2/extra/httpd-dav.conf   OK
    testing: etc/apache2/extra/httpd-default.conf   OK
    testing: etc/   OK
    testing: etc/sshd_config          OK
    testing: etc/sshd_config~previous   OK
    testing: etc/syslog.conf          OK
    testing: etc/ttys                 OK
    testing: etc/xtab                 OK
    testing: etc/zshenv               OK
No errors detected in compressed data of

See unzip command options

Type the following command:

unzip -h

Sample outputs:

UnZip 5.52 of 28 February 2005, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using; see README for details.
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
  -x  exclude files that follow (in xlist)   -d  extract files into exdir
modifiers:                                   -q  quiet mode (-qq => quieter)
  -n  never overwrite existing files         -a  auto-convert any text files
  -o  overwrite files WITHOUT prompting      -aa treat ALL files as text
  -j  junk paths (do not make directories)   -v  be verbose/print version info
  -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
Examples (see unzip.txt for more info):
  unzip data1 -x joe   => extract all files except joe from zipfile
  unzip -p foo | more  => send contents of via pipe into program more
  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.