Compress and Uncompress Files with zip Command in Linux

In this tutorial, you are going to learn how to compress and uncompress files with zip command in Linux. zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS/2, Windows 9x/NT/XP, Minix, Atari, Macintosh, Amiga, and Acorn RISC OS. zip command, apart from packaging and compression of files it is also used for archiving files. To uncompress files in Linux, use unzip command. It list, test and extract compressed files in a ZIP archive

Zip Syntax:

zip [options] zipfile file...

Unzip Syntax:

unzip [options] zipfile file...

Compress and Uncompress Files with zip Command in Linux

In Ubuntu distros, by default zip and unzip utilities are installed. If not installed open your terminal and run the following commands in order to install them.

Debian or Ubuntu Linux;

Install zip,

sudo apt install zip

install uzip,

sudo apt install unzip

RHEL or CentOS Linux;

Install zip,

sudo yum install zip

Install unzip,

sudo yum install unzip

Fedora Linux 22 and above;

Install zip;

sudo dnf install zip

Install unzip;

sudo dnf install unzip

Compress Files with zip command in Linux

Compress Single Files

Use the following command to zip a single file in the current directory;

zip users.zip user1.txt
adding: user1.txt (deflated 5%)

Check if the zip file has been generated,

ls -lh
total 24K
-rw-rw-r-- 1 thehero thehero  42 Mar 29 00:27 user1.txt
-rw-rw-r-- 1 thehero thehero  91 Mar 29 00:28 user2.txt
-rw-rw-r-- 1 thehero thehero 115 Mar 29 00:28 user3.txt
-rw-rw-r-- 1 thehero thehero 150 Mar 29 00:28 user4.txt
-rw-rw-r-- 1 thehero thehero 203 Mar 29 00:29 user5.txt
-rw-rw-r-- 1 thehero thehero 208 Mar 29 00:32 users.zip

the file has been created, we have users.zip file in the output above.

Compress Several Files with zip command in Linux

zip can compress multiple file at once, use the following command to accomplish the task;

zip multiple.zip user1.txt user2.txt user3.txt user4.txt user5.txt
  adding: user1.txt (deflated 5%)
  adding: user2.txt (deflated 11%)
  adding: user3.txt (deflated 13%)
  adding: user4.txt (deflated 18%)
  adding: user5.txt (deflated 22%)

confirm if the multiple.zip has been created,

ls -lh
total 28K
-rw-rw-r-- 1 thehero thehero 1.3K Mar 29 09:57 multiple.zip
-rw-rw-r-- 1 thehero thehero   42 Mar 29 00:27 user1.txt
-rw-rw-r-- 1 thehero thehero   91 Mar 29 00:28 user2.txt
-rw-rw-r-- 1 thehero thehero  115 Mar 29 00:28 user3.txt
-rw-rw-r-- 1 thehero thehero  150 Mar 29 00:28 user4.txt
-rw-rw-r-- 1 thehero thehero  203 Mar 29 00:29 user5.txt

For the files which have the same extension, i.e (.txt) use wildcard (*) instead of typing all files. This make work easier and save time.

zip multiple.zip *.txt
updating: user1.txt (deflated 5%)
updating: user2.txt (deflated 11%)
updating: user3.txt (deflated 13%)
updating: user4.txt (deflated 18%)
updating: user5.txt (deflated 22%)

Confirm the zip file created,

ls -lh
total 28K
-rw-rw-r-- 1 thehero thehero 1.3K Mar 29 10:08 multiple.zip
-rw-rw-r-- 1 thehero thehero   42 Mar 29 00:27 user1.txt
-rw-rw-r-- 1 thehero thehero   91 Mar 29 00:28 user2.txt
-rw-rw-r-- 1 thehero thehero  115 Mar 29 00:28 user3.txt
-rw-rw-r-- 1 thehero thehero  150 Mar 29 00:28 user4.txt
-rw-rw-r-- 1 thehero thehero  203 Mar 29 00:29 user5.txt

Compress a Directory with Zip command in Linux

To compress a directory, use zip command with option -r (recursive) i.e  zip -r zipfile directoryname

zip -r contents.zip ~/Downloads
  adding: home/thehero/Downloads/ (stored 0%)
  adding: home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf (deflated 5%)
  adding: home/thehero/Downloads/vagrant_2.2.14_x86_64.deb (deflated 1%)
  adding: home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf (deflated 26%)
  adding: home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run (deflated 1%)
  adding: home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf (deflated 17%)

use ls -lh to confirm the zip file,

ls -lh
-rw-rw-r-- 1 thehero thehero 206M Mar 29 10:19 contents.zip

Update an Existing zip in Linux

To add add a new file to an existing zip file, use zip command with -u option i.e zip -u zipfile file

zip -u users.zip user6.txt
 adding: user6.txt (deflated 22%)

user6.txt has been added to users.zip

Delete a File from a zip file in Linux

You can as well remove a file in an existing zip file, use zip command with -d option i.e zip -d zipfile file

zip -d users.zip user2.txt
deleting: user1.txt

user1.txt has been deleted from users.zip

Compress Files Quietly with zip command

To zip files in a quite operation that is, do not display the output when the compression is taking place use zip command with -q option i.e zip -q zipfile file

zip -q instance.zip *.txt

In the above command, we get no output but if we check the zip files with ls -ah you find that instance.zip file has been created.

ls -lh
-rw-rw-r-- 1 thehero thehero 1.3K Mar 29 11:17 instance.zip

Compress a File to a Different Directory

To compress a file to a different directory provide a path to a directory where you want to zip a file to. i.e zip /path/to/directoryzipfile file

zip ~/Downloads/users.zip *.txt
  adding: user1.txt (deflated 5%)
  adding: user3.txt (deflated 13%)
  adding: user4.txt (deflated 18%)
  adding: user5.txt (deflated 22%)
  adding: user6.txt (deflated 22%)

users.zip file has been created in ~/Downloads directory

Delete Original Files After compression

By default, zip maintain original files after compression. To delete original files after compression use zip command with -m option i.e zip -m zipfile file

zip -m users.zip *.txt
updating: user6.txt (deflated 22%)
  adding: user1.txt (deflated 5%)
  adding: user3.txt (deflated 13%)
  adding: user4.txt (deflated 18%)
  adding: user5.txt (deflated 22%)

With ls -lh command we can see the output is user.zip file without .txt files which means the files have been deleted.

ls -lh
-rw-rw-r-- 1 thehero thehero 1.3K Mar 29 11:48 users.zip

Compress Files with Different Levels

When compressing files, we can set different types of compression. The levels ranges from 0-9 with 9 being the highest level and 0 being the lowest level.

Compress Files in Low Level

Use the following command to compress files in low level.

zip -0 users1.zip *.txt
updating: user6.txt (stored 0%)
updating: user1.txt (stored 0%)
updating: user3.txt (stored 0%)
updating: user4.txt (stored 0%)
updating: user5.txt (stored 0%)
Compress Files in High Level

Use the following command to compress files in high level.

zip -9 users.zip *.txt
  adding: user1.txt (deflated 5%)
  adding: user3.txt (deflated 13%)
  adding: user4.txt (deflated 18%)
  adding: user5.txt (deflated 22%)
  adding: user6.txt (deflated 22%)

We can compare the output of low and high level.

ls -lh
-rw-rw-r-- 1 thehero thehero 1.5K Mar 29 12:19 users1.zip
-rw-rw-r-- 1 thehero thehero 1.3K Mar 29 12:19 users.zip

In the above output,we see that different sizes of different levels are obtained. users.zip compressed with high level has smaller size as compared to users1.zip compressed with low level has bigger size.

Compress Files With Encryption

To create a zip file protected with a password, use zip command with -e option i.e zip -e zipfile file. When creating a zip file of this kind, you will be prompted to enter a password and also you have to verify the password to confirm.

zip -e encrypted.zip *.txt
Enter password: 
Verify password: 
  adding: user1.txt (deflated 5%)
  adding: user3.txt (deflated 13%)
  adding: user4.txt (deflated 18%)
  adding: user5.txt (deflated 22%)
  adding: user6.txt (deflated 22%)

Add Password to Compressed File

To encrypt previously created zip file which was not encrypted or protected with a password, use zipcloak command i.e zipcloak zipfile

zipcloak users.zip
Enter password: 
Verify password: 
encrypting: user1.txt
encrypting: user3.txt
encrypting: user4.txt
encrypting: user5.txt
encrypting: user6.txt

When you run the above command you will be prompted to enter a password and verify it for the zip file which is users.zip

Search For Files Inside Compressed File

To search files in a ZIP archive for lines matching a pattern, use zipgrep command. zipgrep will search files within a ZIP archive for lines matching the given string or pattern. i.e

zipgrep pattern zipfile
zipgrep love users2.zip
user2.txt: i love linux and it distributions

Display Details of a Compressed File

To display the internal structure of zip files, use zipdetails command. Zipdetails displays information about the internal record structure of the zip file. i.e zipdetails zipfile

zipdetails users2.zip
0000 LOCAL HEADER #1       04034B50
0004 Extract Zip Spec      14 '2.0'
0005 Extract OS            00 'MS-DOS'
0006 General Purpose Flag  0000
     [Bits 1-2]            0 'Normal Compression'
0008 Compression Method    0008 'Deflated'
000A Last Mod Time         527D0366 'Mon Mar 29 00:27:12 2021'
000E CRC                   1D3B3074
0012 Compressed Length     00000028
0016 Uncompressed Length   0000002A
001A Filename Length       0009
001C Extra Length          001C
001E Filename              'user1.txt'
0027 Extra ID #0001        5455 'UT: Extended Timestamp'
0029   Length              0009
002B   Flags               '03 mod access'
002C   Mod Time            6060F4B0 'Mon Mar 29 00:27:12 2021'
0030   Access Time         60619A56 'Mon Mar 29 12:13:58 2021'
0034 Extra ID #0002        7875 'ux: Unix Extra Type 3'
0036   Length              000B
0038   Version             01
0039   UID Size            04
003A   UID                 000003E8
003E   GID Size            04
003F   GID                 000003E8
0043 PAYLOAD               .J...K.R....+...K.(.v.v.....r... .......

Display Detailed Information a Compressed Files

To list detailed information about a ZIP archive, use zipinfo command. Zipinfo lists technical information about files in a ZIP archive.

Such information includes file access permissions, encryption status, type of compression, version and operating system or file system of compressing program, and the like. zipinfo zipfile

zipinfo users.zip
Archive:  users.zip
Zip file size: 1572 bytes, number of entries: 6
-rw-rw-r--  3.0 unx       42 Tx defX 21-Mar-29 00:27 user1.txt
-rw-rw-r--  3.0 unx      115 Tx defX 21-Mar-29 00:28 user3.txt
-rw-rw-r--  3.0 unx      150 Tx defX 21-Mar-29 00:28 user4.txt
-rw-rw-r--  3.0 unx      203 Tx defX 21-Mar-29 00:29 user5.txt
-rw-rw-r--  3.0 unx      203 Tx defX 21-Mar-29 10:33 user6.txt
-rw-rw-r--  3.0 unx       35 tx stor 21-Mar-29 13:19 user2.txt
6 files, 748 bytes uncompressed, 614 bytes compressed:  17.9%

Uncompress Files with zip command in Linux

Uncompress Single Files

Use the following command to unzip a single file in the current directory;

unzip users2.zip
Archive:  users1.zip
 extracting: user1.txt               
 extracting: user3.txt               
 extracting: user4.txt               
 extracting: user5.txt               
 extracting: user6.txt

Uncompress Several Files

unzip command can uncompress multiple file at once, use the following command to unzip files with the same file extension i.e .zip in the current directory.

unzip '*.zip'
Archive:  users1.zip
 extracting: user1.txt               
 extracting: user3.txt               
 extracting: user4.txt               
 extracting: user5.txt               
 extracting: user6.txt
 
Archive:  contents.zip
  inflating: home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf  
  inflating: home/thehero/Downloads/vagrant_2.2.14_x86_64.deb  
  inflating: home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf  
  inflating: home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run  
  inflating: home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

2 archives were successfully processed.              

Uncompress a File to a Different Directory

With unzip command, you uncompress zip files to a different directory instead of current directory. i.e unzip zipfile -d /path/to/directory

unzip users2.zip -d ~/Documents 
Archive:  users2.zip
  inflating: /home/thehero/Documents/user1.txt  
 extracting: /home/thehero/Documents/user2.txt  
  inflating: /home/thehero/Documents/user3.txt  
  inflating: /home/thehero/Documents/user4.txt  
  inflating: /home/thehero/Documents/user5.txt  
  inflating: /home/thehero/Documents/user6.txt

Uncompress Files Quietly

To unzip files in a quite operation that is, do not display the output when the compression is taking place use unzip command with -q option i.e unzip -q zipfile

unzip -q users2.zip

In the above command, we get no output but if we check the our current directory files with ls -ah you find that users2.zip file has been unzipped.

ls -lh
-rw-rw-r-- 1 thehero thehero   42 Mar 29 00:27 user1.txt
-rw-rw-r-- 1 thehero thehero   35 Mar 29 13:19 user2.txt
-rw-rw-r-- 1 thehero thehero  115 Mar 29 00:28 user3.txt
-rw-rw-r-- 1 thehero thehero  150 Mar 29 00:28 user4.txt
-rw-rw-r-- 1 thehero thehero  203 Mar 29 00:29 user5.txt
-rw-rw-r-- 1 thehero thehero  203 Mar 29 10:33 user6.txt

Uncompress Files With Encryption

To uncompress a zip file protected with a password, use unzip command with -P option i.e unzip -P <password> zipfile.

unzip -P 1234 encrypted.zip
Archive:  encrypted.zip
  inflating: user1.txt               
  inflating: user3.txt               
  inflating: user4.txt               
  inflating: user5.txt               
  inflating: user6.txt               

Overwrite Files When Uncompressing

When uncompressing file in a directory where the same file already existed, you will be prompted to: overwrite the current file, skip unzipping of the current file, overwrite all files, skip unzipping of all files, or rename the file.

unzip users2.zip
Archive:  users2.zip
replace user1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: 

use -o option to overwrite all existing files without prompting. i.e unzip -o zipfile

unzip -o users2.zip
Archive:  users2.zip
  inflating: user1.txt               
 extracting: user2.txt               
  inflating: user3.txt               
  inflating: user4.txt               
  inflating: user5.txt               
  inflating: user6.txt

Uncompress Files Excluding Some Files

Use -x option when uncompressing a zip file if you want to exclude some files. i.e unzip zipfile -x file

unzip users2.zip -x user3.txt
Archive:  users2.zip
  inflating: user1.txt               
 extracting: user2.txt               
  inflating: user4.txt               
  inflating: user5.txt               
  inflating: user6.txt

View Uncompressed File Contents

Use -l option with the unzip command, to list the content of a zip file without extracting it. i.e unzip -l zipfile

unzip -l users2.zip
Archive:  users2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       42  2021-03-29 00:27   user1.txt
       35  2021-03-29 13:19   user2.txt
      115  2021-03-29 00:28   user3.txt
      150  2021-03-29 00:28   user4.txt
      203  2021-03-29 00:29   user5.txt
      203  2021-03-29 10:33   user6.txt
---------                     -------
      748                     6 files

That’s all about our tutorial on how to Compress and Uncompress Files with zip Command in Linux, for more info about zip and unzip command feel free to check their man pages utilities respectively. Stay tuned for the next tutorial.

man zip
man unzip

You can also check;

Compress and Uncompress Files with tar Command in Linux

Leave a Comment