Best Zip Command With Examples In Linux
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 zipping like compress so many files into 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 an 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 its the size.

Best Zip Commands With Examples In Linux
Best Zip Command With Examples In Linux:
So In this article, we are going to discuss briefly zip Commands.
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 the below command.
[[email protected] ~]# zip doc.zip doc.txt adding: doc.txt (stored 0%) [[email protected] ~]# ls anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog [[email protected] ~]#
2. Compress Multiple 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.
[[email protected] ~]# ls anaconda-ks.cfg doc1.txt doc3.txt doc5.txt install.log.syslog Desktop doc2.txt doc4.txt install.log [[email protected] ~]# 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%) [[email protected] ~]# ls anaconda-ks.cfg doc1.txt doc3.txt doc5.txt install.log Desktop doc2.txt doc4.txt doc.zip install.log.syslog [[email protected] ~]#
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 the zip command with Option x
Example :
[[email protected] ~]# ls mydata/ doc1.txt doc3.txt doc5.txt file2.pdf file4.pdf doc2.txt doc4.txt file1.pdf file3.pdf file5.pdf [[email protected] ~]# 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%) [[email protected] ~]# 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. [[email protected] ~]# 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 [[email protected] ~]#
4. Compress a Directory using the zip command
Here we have a Directory named mydata with some files, Let’s Compress the directory using the below command.
Example :
[[email protected] ~]# ls mydata/ test1.txt test2.txt test3.txt test4.txt [[email protected] ~]# 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%) [[email protected] ~]# ls anaconda-ks.cfg file install.log.syslog mydata.zip Desktop install.log mydata [[email protected] ~]#
Also Read – Compress Files Using Bz2 (Bzip2 Linux Command) And Unzip Bz2 File In Linux
5. List Files & Directories in zip Archive
To List Files and Directories which is Compressed in a zip archive use the below command.
Method: 1
We can do this using the zip command with Option l
[[email protected] ~]# 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 the zip archive as shown below.
[[email protected] ~]# 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 the below command.
[[email protected] ~]# ls mydata/ database doc1.txt doc2.txt doc3.txt doc4.txt doc5.txt [[email protected] ~]# ls mydata/database/ file1.txt file2.txt file3.txt file4.txt file5.txt [[email protected] ~]# 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%) [[email protected] ~]#
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
[[email protected] ~]# ls anaconda-ks.cfg data.zip Desktop install.log install.log.syslog [[email protected] ~]# 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. [[email protected] ~]# touch test.txt [[email protected] ~]# ls anaconda-ks.cfg data.zip Desktop install.log install.log.syslog test.txt [[email protected] ~]# mv test.txt test.zip [[email protected] ~]# 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. [[email protected] ~]# 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) [[email protected] ~]#
As we can see above it’s not a valid zip file as we didn’t create it by using the 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 the 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.
[[email protected] ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip [[email protected] ~]# 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) [[email protected] ~]# 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. [[email protected] ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata mydata.zip [[email protected] ~]# ls mydata doc1.txt [[email protected] ~]#
Note: You have to mention the complete Path to Extract a File or a Directory as it was Zipped As I mentioned here mydata/doc1.txt.
9. Unzip Multiple File from zip Archive
We also can extract Multiple files from zip Archive using the below command, Here I am extracting two files file5.txt and file4.txt.
Example :
[[email protected] ~]# unzip mydata.zip mydata/database/file4.txt mydata/database/file5.txt Archive: mydata.zip extracting: mydata/database/file5.txt extracting: mydata/database/file4.txt [[email protected] ~]# ls mydata/database/ file4.txt file5.txt [[email protected] ~]#
Also Read – Best Linux Gzip Command (Gzip Compression) With Examples
10. Unzip Multiple Files using a WildCard
Suppose you want to extract so many files of the same type, for example, you want to extract some file with extension .txt, We can do that using Wildcard as shown in the below.
Example :
[[email protected] ~]# 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 [[email protected] ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata mydata.zip [[email protected] ~]# ls mydata database doc1.txt doc2.txt doc3.txt doc4.txt doc5.txt [[email protected] ~]# ls mydata/database/ file1.txt file2.txt file3.txt file4.txt file5.txt [[email protected] ~]#
11. Unzip to a Different Directory
To Extract a zip archive to a different directory we can use the unzip command with option d, Here I am extracting a zip archive named mydata.zip to Directory named /testing.
Example :
[[email protected] ~]# mkdir /testing [[email protected] ~]# 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 [[email protected] ~]# ls /testing/ mydata [[email protected] ~]# ls /testing/mydata/ database doc1.txt doc2.txt doc3.txt doc4.txt doc5.txt [[email protected] ~]# ls /testing/mydata/database/ file1.txt file2.txt file3.txt file4.txt file5.txt [[email protected] ~]#
12. Delete a File from zip Archive
We can delete a Particular file from the zip archive using the zip command with option d, Here I am deleting a file named doc1.txt from zip archive mydata.zip.
Example :
[[email protected] ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip [[email protected] ~]# 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) [[email protected] ~]# 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 the zip command with options P as shown in the command below.
Example :
[[email protected] ~]# ls anaconda-ks.cfg Desktop doc.txt install.log install.log.syslog [[email protected] ~]# zip -P pass123 doc.zip doc.txt # Create a zip file with Password Protected adding: doc.txt (stored 0%) [[email protected] ~]# ls anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog [[email protected] ~]# rm doc.txt rm: remove regular empty file `doc.txt'? y [[email protected] ~]# 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 [[email protected] ~]# ls anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog [[email protected] ~]#
14. Encrypt the zip Archive with Its Content
To encrypt a zip file with all its content we can compress the file using the zip command with option e, As shown below. During Encryption of the zip archive it will ask for a password, So enter it Twice as shown below.
Example :
[[email protected] ~]# ls anaconda-ks.cfg Desktop doc.txt install.log install.log.syslog [[email protected] ~]# zip -e doc.zip doc.txt # Encrypt a zip file Enter password: Verify password: adding: doc.txt (stored 0%) [[email protected] ~]# ls anaconda-ks.cfg Desktop doc.txt doc.zip install.log install.log.syslog [[email protected] ~]#
15. Add a File to Existing zip Archive
Suppose we have a zip archive and want to add a file to the existing zip file.
Example :
Here in this example I have a zip file named mydata.zip and I am going to add a file named test.txt to my existing zip archive, So Follow the steps.
[[email protected] ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip [[email protected] ~]# 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 [[email protected] ~]# touch test.txt [[email protected] ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog mydata.zip test.txt [[email protected] ~]# zip mydata.zip test.txt # Add file to Existing zip Archive adding: test.txt (stored 0%) [[email protected] ~]# 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
Also Read – How To Install 7Zip (7z) Archive Tool In Ubuntu
16. Different Compression Level
There is a Different compression level available for zipping 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 :
[[email protected] ~]# 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%) [[email protected] ~]# 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 a zip archive
Here I archived a text file using zip which contains some text, we can see that without extracting the file using zcat command as shown below.
Example :
[[email protected] ~]# ls anaconda-ks.cfg Desktop doc.zip install.log install.log.syslog [[email protected] ~]# zcat doc.zip # To Check the content of the file Welcome to itsmarttricks.com Subscribe us for Updated Linux Tutorials, Guides, Tips and Tricks... [[email protected] ~]# # 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. [[email protected] ~]# zip apache.zip /etc/httpd/conf/httpd.conf adding: etc/httpd/conf/httpd.conf (deflated 65%) [[email protected] ~]# ls anaconda-ks.cfg apache.zip Desktop install.log install.log.syslog [[email protected] ~]# zless apache.zip

Zip log file
Also, we can use zmore command to check the log file which is the same as zless but here we can’t use the 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.
[[email protected] ~]# zmore apache.zip

Zip log file
18. Update existing files in a 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. [[email protected] ~]# ls mydata/ doc1.txt doc2.txt doc3.txt [[email protected] ~]# cat mydata/doc1.txt Welcome to itsmarttricks.com # Now Create a Zip File of Directory mydata using below command [[email protected] ~]# 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%) [[email protected] ~]# 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. [[email protected] ~]# cat mydata/doc1.txt Welcome to itsmarttricks.com Get 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 [[email protected] ~]# zip mydata.zip -rf mydata/ # Update the File in Existing zip Archive freshening: mydata/doc1.txt (deflated 7%) [[email protected] ~]# # Now Let's delete our Source Directory "mydata" and extract the zip file to check the file doc1.txt is Updated on not. [[email protected] ~]# rm -rf mydata # Delete the Source Directory [[email protected] ~]# ls anaconda-ks.cfg Desktop file install.log install.log.syslog mydata.zip [[email protected] ~]# 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 [[email protected] ~]# ls anaconda-ks.cfg Desktop file install.log install.log.syslog mydata mydata.zip [[email protected] ~]# cat mydata/doc1.txt Welcome to itsmarttricks.com Get All The Latest Updates Delivered Straight Into Your Inbox For Free! [[email protected] ~]# # 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 the zip file in Different Directory
- d – To Delete a file from Existing zip File
Note: The argument “d” we have used on a 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
That’s all, In this article, we have explained the Best Zip Command With Examples In Linux. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.