How to zip directory in linux explained with examples

Zip is command in linux is used for compressing files and directory.In this post we will learn how to zip the directory or folder in linux.

To compress directory use the below given format

zip -r give-file-name.zip Directory-name

example:

root@tuxworld:/opt# ls -ld teamviewer8/
drwxr-xr-x 6 root root 4096 Oct  2 08:43 teamviewer8/
root@tuxworld:/opt# zip -r teamviewer8.zip teamviewer8

To compress better and faster

To compress directory faster and better we can use the option -9 for better compress and -1 for faster.

example:

root@tuxworld:/opt# zip -1 -9 -r teamviewer8.zip teamviewer8

To zip multiple directory

You can also zip the directory by given below format

zip -r Give-filename.zip dir1 dir2 dir3 dirN

example:
In given below example,here I gave file name as twodir.zip and I am compressing two different directory called
testdir and teamviewer8

zip -r twodir.zip testdir teamviewer8

How to unzip

To decompress or unzip the .zip format file,you have to use simple command called unzip
Format:

unzip filename.zip

example:

unzip twodir.zip

How to list items inside zipped file

This command is useful when you want to only see the listed items inside zipped file without decompressing it.

Format:

unzip -l filename.zip

example:

root@tuxworld:/tmp# unzip -l mydir.zip 
Archive:  mydir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2013-10-18 20:19   test/
       14  2013-10-18 20:19   test/file3
       14  2013-10-18 20:19   test/file2
        0  2013-10-18 20:19   test/file4
       14  2013-10-18 20:19   test/file1
        0  2013-07-31 02:00   testdir/
        0  2013-07-31 02:00   testdir/testfile
        0  2013-07-31 01:12   testdir/hgjh
---------                     -------
       42                     8 files
root@tuxworld:/tmp# 

how to test zipped file

It is good to test the file before unzip, doing test give you two advantages first you got a health check of zipped file and second you also got to know the list of directory and file inside zipped file without extracting.

To test the zip file use the option -t in given below format

format:

unzip -t filename.zip

Example:

root@tuxworld:/opt# ls -ld test testdir/
drwxr-xr-x 2 root root 4096 Oct 18 20:19 test
drwxrwx--- 2 joe  hr   4096 Jul 31 02:00 testdir/
root@tuxworld:/opt# zip -r mydir.zip test testdir/
  adding: test/ (stored 0%)
  adding: test/file3 (stored 0%)
  adding: test/file2 (stored 0%)
  adding: test/file4 (stored 0%)
  adding: test/file1 (stored 0%)
  adding: testdir/ (stored 0%)
  adding: testdir/testfile (stored 0%)
  adding: testdir/hgjh (stored 0%)
root@tuxworld:/opt# 
root@tuxworld:/opt# ls -l mydir.zip 
-rw-r--r-- 1 root root 1250 Oct 18 20:20 mydir.zip
root@tuxworld:/opt# 
root@tuxworld:/opt# unzip -t mydir.zip 
Archive:  mydir.zip
    testing: test/                    OK
    testing: test/file3               OK
    testing: test/file2               OK
    testing: test/file4               OK
    testing: test/file1               OK
    testing: testdir/                 OK
    testing: testdir/testfile         OK
    testing: testdir/hgjh             OK
No errors detected in compressed data of mydir.zip.
root@tuxworld:/opt#

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.