Table of Contents

How to Zip Files and Directories in Linux

Zip is very useful utility to compress data to

  • Save disk space 
  • To transfer big files faster

Zip is also used to split files.

In Linux it is very easy to use zip utility. Lets install zip. In Centos do following...

sudo yum -y install zip

I got following message...

Package zip-3.0-11.el7.x86_64 already installed and latest version

If it is not installed then, it will be installed.

Lets check what are the options available for zip command

zip --help
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help

There are lot of options that come with zip. We will start with the basics of zipping files and directories and touch upon few of these switches which one should know.

How to Compress a file in Linux

Lets compress a file first and see how it works

ls -lh credits.csv
-rw-rw-r-- 1 root root 182M Sep 21 10:29 credits.csv

As we see above the size of file is 182M. Lets compress it using zip and see what we get.

zip credits credits.csv
adding: credits.csv (deflated 76%)

deflated 76% means, file has been compressed by 76%. Lets check the size of file now

ls -lh credits.zip
-rw-rw-r-- 1 root root 44M Nov 29 02:02 credits.zip

There you go, size of file is 44M which is 24% of original size.

Lets try the switch -q quite operation. We should not see any message if we use this switch.

zip -q credits credits.csv

Yes we didn't see the deflated message.

Lets try the switch -1 according to the documentation it says compress faster but it compression is not as much, lets try that.

zip -1 credits credits.csv
updating: credits.csv (deflated 72%)

If you run above, you will see it runs relative faster but compression is only 72% as compared to 76% compression we got earlier.

There is another switch -9 which is used to compress better, so there should be relatively slower. Lets try that and see what is the compress we get.

zip -9 credits credits.csv
updating: credits.csv (deflated 77%)

Indeed the compression is marginally better 77% as compared to 76%.

Lets try -u switch now. Which updates only if file has changed. To see it we will have to time the operation.Lets try on our file and see what happens. 

time zip -u credits credits.csv
real	0m0.004s
user	0m0.000s
sys	0m0.000s

As we see above, it just finished instantly. it means zip didnt redo the operation since credits.csv not changed and we already had the credits.zip file present.

-v is to add more verbose information as shown below.

zip -vu credits credits.csv
zip diagnostic: credits.csv up to date

As we see above, I used both the switches together in the above command -u and -v. We got a message "zip diagnostic: credits.csv up to date" because of -v switch.

How to zip directory and its files in Linux

ls admin/
admin   controllers  jest.config.js  node_modules  package-lock.json  scripts
config  doc          LICENSE.md      package.json  README.md

Ok In the directory admin, as we can see there are some files and directories. Lets try to zip the whole directory admin using -r switch.

zip -qr admin admin
ls -lh admin.zip
-rw-r--r-- 1 root root 26M Nov 29 02:31 admin.zip

As we see above, admin.zip file has been created whose size is 26M. Lets preview the contents of this zip file without using the unzip command. We can do that either using less editor command or another utility which is zipinfo.

zipinfo -l admin.zip | head -5
Archive:  admin.zip
Zip file size: 27078266 bytes, number of entries: 1988
drwxr-xr-x  3.0 unx        0 bx        0 stor 19-May-01 21:53 admin/
-rw-r--r--  3.0 unx       72 tx       70 defN 19-May-01 21:52 admin/jest.config.js
drwxr-xr-x  3.0 unx        0 bx        0 stor 19-May-01 21:52 admin/controllers/

I am just printing out the first 5 file names in the zipped file just to show you guys that files have been zipped.

Lets try -d switch, this is used to delete entries in the zipped file. Lets try to delete the file admin/jest.config.js from the zipped file.

zip -d admin.zip "admin/jest.config.js"
deleting: admin/jest.config.js

There you go, we got the message that, file has been deleted. lets use zipinfo to see if it is still there.

zipinfo -l admin.zip | egrep 'jest.config.js' | wc -l 
0

Ok as wee see above, the file is not present in the admin.zip anymore.

That's it for now. Hope the example of above commands will be helpful.


Related Posts

1