Hack 44. Zip command basics

How to zip multiple files?

syntax: zip {.zip file-name} {file-names}

# zip var-log-files.zip /var/log/*
  adding: var/log/acpid (deflated 81%)
  adding: var/log/anaconda.log (deflated 79%)
  adding: var/log/anaconda.syslog (deflated 73%)
  adding: var/log/anaconda.xlog (deflated 82%)
  adding: var/log/audit/ (stored 0%)
  adding: var/log/boot.log (stored 0%)
  adding: var/log/boot.log.1 (deflated 40%)
  adding: var/log/boot.log.2 (deflated 42%)
  adding: var/log/boot.log.3 (deflated 40%)
  adding: var/log/boot.log.4 (deflated 40%)

How to zip a directory and it’s files recursively?

# zip -r var-log-dir.zip /var/log/
updating: var/log/ (stored 0%)
  adding: var/log/wtmp (deflated 78%)
  adding: var/log/scrollkeeper.log (deflated 94%)
  adding: var/log/rpmpkgs.3 (deflated 68%)
  adding: var/log/spooler (stored 0%)
  adding: var/log/cron.2 (deflated 90%)
  adding: var/log/spooler.1 (stored 0%)
  adding: var/log/spooler.4 (stored 0%)
  adding: var/log/httpd/ (stored 0%)
  adding: var/log/rpmpkgs.1 (deflated 68%)
  adding: var/log/anaconda.log (deflated 79%)
  adding: var/log/secure.2 (deflated 93%)

How to unzip a *.zip compressed file?

# unzip var-log.zip
Archive:  var-log.zip
  inflating: var/log/acpid
  inflating: var/log/anaconda.log
  inflating: var/log/anaconda.syslog
  inflating: var/log/anaconda.xlog
   creating: var/log/audit/

To see a detailed output during unzip pass the –v option as shown below.

# unzip -v var-log.zip

Archive:  var-log.zip
 Length   Method    Size  Ratio   Date   Time   CRC-32    Name
--------  ------  ------- -----   ----   ----   ------    ----
    1916  Defl:N      369  81%  02-08-08 14:27  e2ffdc0c  var/log/acpid
   13546  Defl:N     2900  79%  02-02-07 14:25  34cc03a1  var/log/anaconda.log

skip..

    7680  Defl:N      411  95%  12-30-08 10:55  fe876ee9  var/log/wtmp.1
   40981  Defl:N     7395  82%  02-08-08 14:28  6386a95e  var/log/Xorg.0.log
--------          -------  ---                            -------
41406991          2809229  93%                            56 files

How to list a content of zip file with uncompressing it?

# unzip -l var-log.zip

Archive:  var-log.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
     1916  02-08-08 14:27   var/log/acpid
    13546  02-02-07 14:25   var/log/anaconda.log

..skip..

    40981  02-08-08 14:28   var/log/Xorg.0.log
    40981  02-08-07 14:56   var/log/Xorg.0.log.old
 --------                   -------
 41406991                   56 files