Hack 44. Zip command basics

by Ramesh

How to zip multiple files?

  1. syntax: zip {.zip file-name} {file-names}
  1. # zip var-log-files.zip /var/log/*
  2. adding: var/log/acpid (deflated 81%)
  3. adding: var/log/anaconda.log (deflated 79%)
  4. adding: var/log/anaconda.syslog (deflated 73%)
  5. adding: var/log/anaconda.xlog (deflated 82%)
  6. adding: var/log/audit/ (stored 0%)
  7. adding: var/log/boot.log (stored 0%)
  8. adding: var/log/boot.log.1 (deflated 40%)
  9. adding: var/log/boot.log.2 (deflated 42%)
  10. adding: var/log/boot.log.3 (deflated 40%)
  11. adding: var/log/boot.log.4 (deflated 40%)

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

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

How to unzip a *.zip compressed file?

  1. # unzip var-log.zip
  2. Archive: var-log.zip
  3. inflating: var/log/acpid
  4. inflating: var/log/anaconda.log
  5. inflating: var/log/anaconda.syslog
  6. inflating: var/log/anaconda.xlog
  7. creating: var/log/audit/

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

  1. # unzip -v var-log.zip
  2.  
  3. Archive: var-log.zip
  4. Length Method Size Ratio Date Time CRC-32 Name
  5. -------- ------ ------- ----- ---- ---- ------ ----
  6. 1916 Defl:N 369 81% 02-08-08 14:27 e2ffdc0c var/log/acpid
  7. 13546 Defl:N 2900 79% 02-02-07 14:25 34cc03a1 var/log/anaconda.log
  8.  
  9. skip..
  10.  
  11. 7680 Defl:N 411 95% 12-30-08 10:55 fe876ee9 var/log/wtmp.1
  12. 40981 Defl:N 7395 82% 02-08-08 14:28 6386a95e var/log/Xorg.0.log
  13. -------- ------- --- -------
  14. 41406991 2809229 93% 56 files

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

  1. # unzip -l var-log.zip
  2.  
  3. Archive: var-log.zip
  4. Length Date Time Name
  5. -------- ---- ---- ----
  6. 1916 02-08-08 14:27 var/log/acpid
  7. 13546 02-02-07 14:25 var/log/anaconda.log
  8.  
  9. ..skip..
  10.  
  11. 40981 02-08-08 14:28 var/log/Xorg.0.log
  12. 40981 02-08-07 14:56 var/log/Xorg.0.log.old
  13. -------- -------
  14. 41406991 56 files