BEST ZIP COMMAND WITH EXAMPLES IN LINUX
Introduction
Zip Command is a tool used in Linux to Compress a file or a Directory to reduce its Size, There are so many other Useful features are there in zip like compress so many files in to a one file, Protect the zip file with Password, Encrypt the zip file and So on.
Let’s take a Scenario Like I have to send a mail with attachment and the file size is 15MB but my attachment size limit is 12MB, in that case i can use zip command to compress the file and reduce it’s size.
So In this Article we are going to discuss briefly on zip Command.
1. Compress a File using zip Command
Compress a file using zip as shown below.
Example :
Here We have a file named doc.txt, Let’s Compress the file using below command.
[root@localhost ~]# zip doc.zip doc.txt
adding: doc.txt (stored 0%)
[root@localhost ~]# ls
anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog
[root@localhost ~]#
2. Compress Multipe Files using zip command
To Compress multiple files use the below command.
Example :
We have Some files like doc1.txt, doc2.txt, doc3.txt, doc4.txt, doc5.txt so let’s Compress it using zip command.
[root@localhost ~]# ls anaconda-ks.cfg doc1.txt doc3.txt doc5.txt install.log.syslog Desktop doc2.txt doc4.txt install.log [root@localhost ~]# zip doc.zip doc1.txt doc2.txt doc3.txt doc4.txt doc5.txt # Compress Multiple Files at Once adding: doc1.txt (stored 0%) adding: doc2.txt (stored 0%) adding: doc3.txt (stored 0%) adding: doc4.txt (stored 0%) adding: doc5.txt (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg doc1.txt doc3.txt doc5.txt install.log Desktop doc2.txt doc4.txt doc.zip install.log.syslog [root@localhost ~]#
3. Exclude files from being zip
I have so many data like Text Files, Office Documents, PDF Files and so on and want to Compress all Files using zip excluding PDF files ( means I don’t want to Compress PDF Files), So follow the below Command to do the same.We can do this using zip command with Option x
Example :
[root@localhost ~]# ls mydata/ doc1.txt doc3.txt doc5.txt file2.pdf file4.pdf doc2.txt doc4.txt file1.pdf file3.pdf file5.pdf [root@localhost ~]# zip mydata.zip -r mydata/ -x *.pdf # Compress all Files Excluding PDF Files adding: mydata/ (stored 0%) adding: mydata/doc1.txt (stored 0%) adding: mydata/doc3.txt (stored 0%) adding: mydata/doc5.txt (stored 0%) adding: mydata/doc4.txt (stored 0%) adding: mydata/doc2.txt (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata mydata.zip # As we can see below on the output all files are Compress Excluding PDF files. [root@localhost ~]# unzip -l mydata.zip # List the Content of zip Archive Archive: mydata.zip Length Date Time Name -------- ---- ---- ---- 0 12-14-16 06:59 mydata/ 0 12-14-16 06:59 mydata/doc1.txt 0 12-14-16 06:59 mydata/doc3.txt 0 12-14-16 06:59 mydata/doc5.txt 0 12-14-16 06:59 mydata/doc4.txt 0 12-14-16 06:59 mydata/doc2.txt -------- ------- 0 6 files [root@localhost ~]#
4. Compress a Directory using zip command
Here we have a Directory named mydata with some files, Let’s Compress the directory using below command.
Example :
[root@localhost ~]# ls mydata/ test1.txt test2.txt test3.txt test4.txt [root@localhost ~]# zip mydata.zip -r mydata/ # Compress a Directory using zip adding: mydata/ (stored 0%) adding: mydata/test2.txt (stored 0%) adding: mydata/test1.txt (stored 0%) adding: mydata/test4.txt (stored 0%) adding: mydata/test3.txt (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg file install.log.syslog mydata.zip Desktop install.log mydata [root@localhost ~]#
5. List Files & Directories in zip Archive
To List Files and Directories which is Compressed in zip archive use the below command.
Method : 1
We can do this using zip command with Option l
[root@localhost ~]# unzip -l documents.zip # List the Content of the zip Archive
Archive: documents.zip
Length Date Time Name
-------- ---- ---- ----
0 12-14-16 06:29 doc1.txt
0 12-14-16 06:29 doc2.txt
0 12-14-16 06:29 doc3.txt
0 12-14-16 06:29 doc4.txt
0 12-14-16 06:29 doc5.txt
-------- -------
0 5 files
Method : 2
OR We can use less command to list the content of zip archive as shown below.
[root@localhost ~]# less documents.zip # List the Content of the zip Archive
# The Output Would Look like as shown below :
Archive: documents.zip 526 bytes 4 files
-rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:40 doc1.txt
-rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:40 doc2.txt
-rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:40 doc3.txt
-rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:40 doc4.txt
4 files, 0 bytes uncompressed, 0 bytes compressed: 0.0%
documents.zip (END)
6. Compress a Directory with all its Files and Sub-Directories
Suppose we have a Directory with Sub-Directories and Files, We can compress it Recursively using zip command with option r.
Example :
Here we have one Directory named mydata with files doc1.txt, doc2.txt….doc5.txt and a Sub-Directory named database with some files file1.txt, file2.txt….file5.txt, So let’s Perform it using below command.
[root@localhost ~]# ls mydata/
database doc1.txt doc2.txt doc3.txt doc4.txt doc5.txt
[root@localhost ~]# ls mydata/database/
file1.txt file2.txt file3.txt file4.txt file5.txt
[root@localhost ~]# zip -r mydata.zip mydata/ # Compress Directory with Sub-Directories with all its file Recursively
adding: mydata/ (stored 0%)
adding: mydata/doc1.txt (stored 0%)
adding: mydata/doc3.txt (stored 0%)
adding: mydata/database/ (stored 0%)
adding: mydata/database/file5.txt (stored 0%)
adding: mydata/database/file3.txt (stored 0%)
adding: mydata/database/file2.txt (stored 0%)
adding: mydata/database/file4.txt (stored 0%)
adding: mydata/database/file1.txt (stored 0%)
adding: mydata/doc5.txt (stored 0%)
adding: mydata/doc4.txt (stored 0%)
adding: mydata/doc2.txt (stored 0%)
[root@localhost ~]#
7. Check Valid zip Archive File
To check if the created zip file is valid or not by using the zip command with option T
[root@localhost ~]# ls anaconda-ks.cfg data.zip Desktop install.log install.log.syslog [root@localhost ~]# zip -T data.zip test of data.zip OK # This is a valid zip File # For Example let's create a text file and change it's extension to .zip by renaming it. [root@localhost ~]# touch test.txt [root@localhost ~]# ls anaconda-ks.cfg data.zip Desktop install.log install.log.syslog test.txt [root@localhost ~]# mv test.txt test.zip [root@localhost ~]# ls anaconda-ks.cfg data.zip Desktop install.log install.log.syslog test.zip # Now Let's check if Renamed zip file is valid or not. [root@localhost ~]# zip -T test.zip zip warning: missing end signature--probably not a zip file (did you zip warning: remember to use binary mode when you transferred it?) zip error: Zip file structure invalid (test.zip) [root@localhost ~]#
As we can see above it’s not a valid zip file as we didn’t created it by using zip command.
8. Unzip a Particular File from zip Archive
I have one zip archive file and I want to Unzip/Extract a Particular file from that, We can do that using below command.
Example :
Here i have a zip Archive named mydata.zip with some files as shown below and I am going to extract the file doc1.txt from that.
[root@localhost ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip [root@localhost ~]# less mydata.zip Archive: mydata.zip 1788 bytes 12 files drwxr-xr-x 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/ -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc1.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc3.txt drwxr-xr-x 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/ -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file5.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file3.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file2.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file4.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file1.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc5.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc4.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc2.txt 12 files, 0 bytes uncompressed, 0 bytes compressed: 0.0% mydata.zip (END) [root@localhost ~]# unzip mydata.zip mydata/doc1.txt # To Unzip Particular File Archive: mydata.zip extracting: mydata/doc1.txt # As we can see below we unzip a file doc1.txt. [root@localhost ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata mydata.zip [root@localhost ~]# ls mydata doc1.txt [root@localhost ~]#
Note : You have to mention the complete Path to Extract a File or a Directory as it was Zipped, Like i mentioned here mydata/doc1.txt.
9. Unzip Multiple File from zip Archive
We also can extract Multiple files from zip Archive using below command, Here I am extracting two files file5.txt and file4.txt.
Example :
[root@localhost ~]# unzip mydata.zip mydata/database/file4.txt mydata/database/file5.txt Archive: mydata.zip extracting: mydata/database/file5.txt extracting: mydata/database/file4.txt [root@localhost ~]# ls mydata/database/ file4.txt file5.txt [root@localhost ~]#
10. Unzip Multiple Files using a WildCard
Suppose you want to extract so many file of same type for example you want to extract some file with extension .txt, We can do that using Wildcard as shown on the below.
Example :
[root@localhost ~]# unzip mydata.zip mydata/*.txt # Extract using WildCard
Archive: mydata.zip
extracting: mydata/doc1.txt
extracting: mydata/doc3.txt
extracting: mydata/database/file5.txt
extracting: mydata/database/file3.txt
extracting: mydata/database/file2.txt
extracting: mydata/database/file4.txt
extracting: mydata/database/file1.txt
extracting: mydata/doc5.txt
extracting: mydata/doc4.txt
extracting: mydata/doc2.txt
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog mydata mydata.zip
[root@localhost ~]# ls mydata
database doc1.txt doc2.txt doc3.txt doc4.txt doc5.txt
[root@localhost ~]# ls mydata/database/
file1.txt file2.txt file3.txt file4.txt file5.txt
[root@localhost ~]#
11. Unzip to a Different Directory
To Extract a zip archive to a different directory we can use unzip command with option d, Here I am extracting zip archive named mydata.zip to Directory named /testing.
Example :
[root@localhost ~]# mkdir /testing
[root@localhost ~]# unzip mydata.zip -d /testing/ # Unzip to a Different Directory
Archive: mydata.zip
creating: /testing/mydata/
extracting: /testing/mydata/doc1.txt
extracting: /testing/mydata/doc3.txt
creating: /testing/mydata/database/
extracting: /testing/mydata/database/file5.txt
extracting: /testing/mydata/database/file3.txt
extracting: /testing/mydata/database/file2.txt
extracting: /testing/mydata/database/file4.txt
extracting: /testing/mydata/database/file1.txt
extracting: /testing/mydata/doc5.txt
extracting: /testing/mydata/doc4.txt
extracting: /testing/mydata/doc2.txt
[root@localhost ~]# ls /testing/
mydata
[root@localhost ~]# ls /testing/mydata/
database doc1.txt doc2.txt doc3.txt doc4.txt doc5.txt
[root@localhost ~]# ls /testing/mydata/database/
file1.txt file2.txt file3.txt file4.txt file5.txt
[root@localhost ~]#
12. Delete a File from zip Archive
We can delete a Particular file from zip archive using zip command with option d, Here I am deleting a file named doc1.txt from zip archive mydata.zip.
Example :
[root@localhost ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip [root@localhost ~]# less mydata.zip # To List Content of the zip Archive Archive: mydata.zip 1788 bytes 12 files drwxr-xr-x 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/ -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc1.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc3.txt drwxr-xr-x 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/ -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file5.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file3.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file2.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file4.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file1.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc5.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc4.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc2.txt 12 files, 0 bytes uncompressed, 0 bytes compressed: 0.0% mydata.zip (END) [root@localhost ~]# zip -d mydata.zip "mydata/doc1.txt" # Delete a File from zip Archive deleting: mydata/doc1.txt Archive: mydata.zip 1648 bytes 11 files drwxr-xr-x 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/ -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc3.txt drwxr-xr-x 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/ -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file5.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file3.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file2.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file4.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:49 mydata/database/file1.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc5.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc4.txt -rw-r--r-- 2.3 unx 0 bx stor 13-Dec-16 06:37 mydata/doc2.txt 11 files, 0 bytes uncompressed, 0 bytes compressed: 0.0% mydata.zip (END)
13. Create a zip file with Password Protected
To create a zip archive with Password Protected we can use zip command with option P as shown on the command below.
Example :
[root@localhost ~]# ls anaconda-ks.cfg Desktop doc.txt install.log install.log.syslog [root@localhost ~]# zip -P pass123 doc.zip doc.txt # Create a zip file with Password Protected adding: doc.txt (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog [root@localhost ~]# rm doc.txt rm: remove regular empty file `doc.txt'? y [root@localhost ~]# unzip doc.zip # Unzip the file Archive: doc.zip [doc.zip] doc.txt password: # Enter the Password to Unzip the File as its Password Protected extracting: doc.txt [root@localhost ~]# ls anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog [root@localhost ~]#
14. Encrypt the zip Archive with It’s Content
To encrypt a zip file with all it’s content we can compress the file using zip command with option e, As shown below.During Encryption of the zip archive it will ask for password, So enter it Twice as shown below.
Example :
[root@localhost ~]# ls anaconda-ks.cfg Desktop doc.txt install.log install.log.syslog [root@localhost ~]# zip -e doc.zip doc.txt # Encrypt a zip file Enter password: Verify password: adding: doc.txt (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog [root@localhost ~]#
15. Add a File to Existing zip Archive
Suppose we have a zip archive and want to add a file to existing zip file.
Example :
Here in this example i have zip file named mydata.zip and I am going to add file named test.txt to my existing zip archive, So Follow the steps.
[root@localhost ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip [root@localhost ~]# unzip -l mydata.zip # List the files Archive: mydata.zip Length Date Time Name -------- ---- ---- ---- 0 12-14-16 07:48 doc1.txt 0 12-14-16 07:48 doc2.txt 0 12-14-16 07:48 doc3.txt -------- ------- 0 3 files [root@localhost ~]# touch test.txt [root@localhost ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip test.txt [root@localhost ~]# zip mydata.zip test.txt # Add file to Existing zip Archive adding: test.txt (stored 0%) [root@localhost ~]# unzip -l mydata.zip Archive: mydata.zip Length Date Time Name -------- ---- ---- ---- 0 12-14-16 07:48 doc1.txt 0 12-14-16 07:48 doc2.txt 0 12-14-16 07:48 doc3.txt 0 12-14-16 07:50 test.txt -------- ------- 0 4 files
16. Different Compression Level
There is Different compression level available for zip like 0, 1 ,2….up to 10, Here we have taken two examples i.e. “0” and “9“.
Level – 0 – For Fast Compression.
Level – 9 – For Best Compression.
Example :
[root@localhost ~]# zip -0 data.zip file* # For Fast Compression adding: file1.txt (stored 0%) adding: file2.txt (stored 0%) adding: file3.txt (stored 0%) adding: file4.txt (stored 0%) adding: file5.txt (stored 0%) [root@localhost ~]# zip -9 data.zip file* # For Best Compression adding: file1.txt (stored 0%) adding: file2.txt (stored 0%) adding: file3.txt (stored 0%) adding: file4.txt (stored 0%) adding: file5.txt (stored 0%)
17. Check the Content of the file in zip Archive
Here I archived a text file using zip which contains some text, we can see that with out extracting the file using zcat command as shown below.
Example :
[root@localhost ~]# ls anaconda-ks.cfg Desktop doc.zip install.log install.log.syslog [root@localhost ~]# zcat doc.zip # To Check the content of the file Welcome to elinuxbook.com Subscribe us for Updated Linux Tutorials, Guides, Tips and Tricks... [root@localhost ~]# # If the file is too long we can use zless command which have the feature to scroll the file by using UP & DOWN arrow to check the file. [root@localhost ~]# zip apache.zip /etc/httpd/conf/httpd.conf adding: etc/httpd/conf/httpd.conf (deflated 65%) [root@localhost ~]# ls anaconda-ks.cfg apache.zip Desktop install.log install.log.syslog [root@localhost ~]# zless apache.zip
Also we can use zmore command to check the long file which is same as zless but here we can’t use UP & DOWN arrow to scroll the file, here we have to Press Space Bar to Scroll the file to DOWN and we can’t go UP once scroll the file to DOWN. zmore command shows one page at a time.
[root@localhost ~]# zmore apache.zip
18. Update existing files in zip Archive
Suppose We have a Directory named mydata which contains some files like doc1.txt, doc2.txt, doc3.txt and we are taking daily backup of it. Out of three files doc1.txt contains some text in it as shown below.
Example :
# As shown below doc1.txt contains some text in it. [root@localhost ~]# ls mydata/ doc1.txt doc2.txt doc3.txt [root@localhost ~]# cat mydata/doc1.txt Welcome to elinuxbook.com # Now Create a Zip File of Directory mydata using below command [root@localhost ~]# zip mydata.zip -r mydata/ adding: mydata/ (stored 0%) adding: mydata/doc1.txt (stored 0%) adding: mydata/doc3.txt (stored 0%) adding: mydata/doc2.txt (stored 0%) [root@localhost ~]# ls anaconda-ks.cfg Desktop file install.log install.log.syslog mydata mydata.zip # After creating a zip file let's update the file doc1.txt and add some more text in it as shown below. [root@localhost ~]# cat mydata/doc1.txt Welcome to elinuxbook.comGet All The Latest Updates Delivered Straight Into Your Inbox For Free!# Now let's Update the file doc1.txt in zip archive without Extracting it using command zip with Optionf [root@localhost ~]# zip mydata.zip -rf mydata/ # Update the File in Existing zip Archive freshening: mydata/doc1.txt (deflated 7%) [root@localhost ~]# # Now Let's delete our Source Directory "mydata" and extract the zip file to check the file doc1.txt is Updated on not. [root@localhost ~]# rm -rf mydata # Delete the Source Directory [root@localhost ~]# ls anaconda-ks.cfg Desktop file install.log install.log.syslog mydata.zip [root@localhost ~]# unzip mydata.zip # Unzip the mydata.zip Archive Archive: mydata.zip creating: mydata/ inflating: mydata/doc1.txt extracting: mydata/doc3.txt extracting: mydata/doc2.txt [root@localhost ~]# ls anaconda-ks.cfg Desktop file install.log install.log.syslog mydata mydata.zip [root@localhost ~]# cat mydata/doc1.txt Welcome to elinuxbook.comGet All The Latest Updates Delivered Straight Into Your Inbox For Free! [root@localhost ~]# # As we can see above the file doc1.txt is updated Successfully
Some Useful zip Command Options :
- x – To Exclude Some files to being Compressed
- r – To Compress Files and Directories Recursively
- d – Extract zip file in Different Directory
- d – To Delete a file from Existing zip File
Note : The argument “d” we have use on different place to work differently, We have Included Both Examples Above.
- T – To Validate zip File
- l – To List the Content of the zip file
- P – To create a zip file with Password Protected
- e – To Encrypt a zip file
- 0 – Compression Level for Fast Compression
- 9 – Compression Level for Best Compression
- f – To Update/Freshening a File in Existing zip File
We tried to include as many as zip command with examples we can, If any commands got missed the Please update us on Comment box below.
If you found this Article useful then Like it, Share it, Subscribe our Site for more Linux Tutorials OR If you have any thing to say then feel free to Comment on Comment Box below.