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

last updated in Categories , ,

I‘m a new Linux system user. I’ve downloaded a file named data.zip 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.

[donotprint][/donotprint]

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 foo.zip

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

unzip foo
OR
<pre lang="bash">
unzip foo.zip

Sample outputs:

:Archive:  foo.zip
   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 file.zip "*.py"
To extract all C source files “*.c”, and “*.h” and a Makefile into the ~/projects/editor directory:

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

Extract all files except bar.txt from zipfile demo.zip

 unzip demo.zip -x bar.txt

Extract files into given directory

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

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

Sample outputs:

Archive:  demo.zip
   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 file.zip
unzip -o file.zip data.jpg

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

unzip -n file.zip
unzip -n file.zip data.jpg

List files stored in a zip file on screen

unzip -l file.zip
unzip -l file.zip | more
unzip -l foo.zip

Sample outputs:

Archive:  foo.zip
  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/ssh_host_rsa_key.pub
     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 data.zip

Sample outputs:

Archive:  data.zip
    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/ssh_host_rsa_key.pub   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 data.zip.

See unzip command options

Type the following command:

unzip 
unzip -h

Sample outputs:

UnZip 5.52 of 28 February 2005, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; 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 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

Posted by: Vivek Gite

The author is the creator of nixCraft 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.

1 comment

    Still, have a question? Get help on our forum!