Archive and Compression – zip

Zip command basics

How to zip multiple files?

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

# 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/
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
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.

# unzip -v

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

# unzip -l

Advanced compression using zip command.

There are 10 levels of compression provided by zip command.
Level 0 is the lowest level, where it just archives the file without any compression.
Level 1 will perform little compression. But, will be very fast.
Level 6 is the default level of compression.
Level 9 is the maximum compression. This will be slower when compared to default level. In my opinion, unless you are
compressing a huge file, you should always use level 9.

In the example below, I used Level 0, default Level 6, and Level 9
compression on a same directory. See the compressed file size yourself.

# zip /var/log/*
# zip -0 /var/log/*
# zip -9 /var/log/*

Password Protection of Zip files

Pass the option –P to the zip command to assign a password to the zip file.

# zip -P mysecurepwd /var/log/*

The above option is good if you are using the command inside a shell-script
for background jobs. However, when you are performing the compression
interactively on the command-line, you don’t want the password to be visible
in the history. So, use the option –e as shown below to assign the password.

# zip -e /var/log/*
Enter password:
Verify password:
updating: var/log/acpid (deflated 81%)
updating: var/log/anaconda.log (deflated 79%)

When you are uncompressing a password protected file, it will ask for the
password as shown below.

# unzip
[] var/log/acpid password:

You may also like