Search anything:

zip command in Linux

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

zip command in Linux allows us to create archive files and directories which need smaller storage sizes and can be sent over low bandwidth networks, zip can also be used to encrypt files to prevent unauthorized access. In this article we discuss compressing and decompressing files among other operations that can be performed with zip on zip files such as splitting, searching, viewing, etc.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Compressing files.
  4. Compressing directories.
  5. Compression levels.
  6. Password protection.
  7. Zip contents.
  8. Searching zip files.
  9. Splitting zip files.
  11. Unzipping(decompressing).
  12. Summary.
  13. References.


Zip is an archive file format that supports lossless compression. We compress files for various reasons e.g minimizing disk space, transfer large files over networks.
Zip files/directories can be decompressed in other operating systems regardless of where it was compressed.

zip command in Linux allows for the creating of these archive files and directories.


The syntax is as follows,


Compressing Files.

To compress a file file.png we write,

zip file.zip file.png

The command will create a new compressed version of file.png with the .zip extension.

  • We can also zip multiple files as follows,
    Create multiple files as follows,
touch file{1..5}.txt

To zip them write,

zip files.zip file1.txt file2.txt file3.txt file4.txt file5.txt

We can also use the wildcard character * as follows,

zip files.zip *.txt

which tells zip to compress all files with the extension .txt.

  • We can also zip files and exclude a specific file as follows,
zip files.zip *.txt -x file2.txt

The command will zip all other files except file2.txt.

We could also opt to exclude all .pdf files, can you write this command?

  • After this operation we will have a .zip file.
    To view its contents, write,
unzip -l files.zip
  • We can also add a file to an already compressed file, as follows,
    Create a another file FILE.txt
touch FILE.txt

To add this file to the files.zip we write,

zip files.zip FILE.txt

Now FILE.txt will be added to files.zip zip file.

  • We can also use the -u option to be safe,
zip -u files.zip FILE.txt

This is useful in occasions whereby we have updated a file and need the changes to reflect in the zipped version.

  • We can also use freshen mode by passing the -f option,
zip -f files.zip FILE.txt

You can test this by adding/removing some text to any of the files then execute the above command to update changes to the compressed version.
When you list the contents unzip -l files.zip you will notice the length has changed.

  • We can also use the -g as follows,
zip -g files.zip file6.txt file7.txt 

The above command adds file6.txt and file7.txt to the already existing zip file files.zip, however if the zip file doesn't exist yet, zip will create a new file.

  • Assuming files.zip was in ~/Documents/Zipped/ directory, we would write,
zip ~/Documents/Zipped/files.zip FILE.txt
  • We use the -d option to remove a file from a zip file,
    An example,
    To remove FILE.txt file from files.zip we write,
zip -d files.zip FILE.txt
  • The -q option which stands for quiet, is used to zip files without displaying text on the terminal,
    Normally the output would be something like,
adding: file1.txt (stored 0%)
adding: file2.txt (stored 0%)
adding: file3.txt (stored 0%)

To avoid displaying such text, we write,

zip -q files.zip file1.txt
  • To display progress in bytes we use the -db option as follows,
zip -db files.zip *.txt

The command will display progress output in bytes.

  • To display it in counts we use the -dc option,
zip -dc files.zip *.txt
  • The -m option is used when we want to replace the original file/files with the zipped version,
zip -m files.zip *.txt

After this operation the only file that will be left remaining in the current directory will be files.zip which replaces all .txt files.

Compressing Directories.

Zip can also be used to compress directories,
An example
To compress a directory DIR we write,

zip -r dir.zip DIR

We use -r option so as to recursively zip the directory along with its subdirectories and files.

Compression levels.

A compression level is responsible for regulating the speed of compression, The highest is 10 the lowest -0, the default compression level for zip is -6.
For example, -0 will indicate no compression, that is zip will store all files.
-1 indicates the fastest compression speed while -9 indicates the slowest compression speed(optimal, ignoring suffix list).

An example
Create a DIR directory and add files with some content,


$ du -sh DIR
12K     DIR/

$ zip -9 dir.zip DIR/
adding: DIR/ (stored 0%)

$ du -sh dir.zip 
4.0K    dir.zip

We can replace -9 with any value from 0-9 to change the compression value.

Password protection.

With zip we can protect(encrypt) files using a password. The -e option is used as follows,

zip -e files.zip *.txt

After which you will be prompted for a password and its confirmation.

zipcloak is used to encrypt files within an existing zip archive that was not previously encrypted, Assuming we have compressed a file and forgot to set the password, We use zipcloak for such a task as follows,

zip files.zip *.txt

Then to implement password protection, we write,

zipcloak files.zip

after which we will be prompted to enter a password and its confirmation.

This is nice but still, contents of zip files can still be listed which might not be desirable, can you think of a way to prevent this?

Zip contents.

The 'unzip -l files.zip' command lists files and directories in a zip file, to list contents of a zip file, zipdetails is used.

zipdetails files.zip

The output will look like this,

0000 LOCAL HEADER #1       04034B50
0004 Extract Zip Spec      0A '1.0'
0005 Extract OS            00 'MS-DOS'
0006 General Purpose Flag  0001
     [Bit  0]              1 'Encryption'
0008 Compression Method    0000 'Stored'
000A Last Mod Time         54417988 'Tue Feb 

The zipinfo command is used to view details pertaining to zip files,

zipinfo files.zip

It displays information such as permissions, size, modification times etc.

Searching zip files.

To search for strings we use zipgrep command which works just like grep but on zip files.
An example

zipgrep searchString files.zip

The command will return all matches found from the zip file.
Assuming we protected it with a password, for each file in the zip file, we will be prompted for a password before the search begins.

Assuming we know the exact file in the zipped file files.zip which might contain the search string, we can search that specific file as follows,

zipgrep searchString files.zip file2.txt

Splitting zip files.

We have compressed a very large directory but the zip file is still considerably large and thus we cannot send it over networks due to size restrictions.
zipsplit command gives us the option to split the file into multiple manageable files using the -n option accompanied by the specified size of chunks.

An example

$ du -sh file.zip 
208M    file.zip

$ zipsplit -n 104857600 file.zip 
3 zip files will be made (100% efficiency)
creating: file1.zip
creating: file2.zip
creating: file3.zip

$ du -sh file.zip file1.zip file2.zip file3.zip 
208M    file.zip
96M     file1.zip
96M     file2.zip
17M     file3.zip

The commands split a file of size 208 MB into three files of sizes 96MB, 96MB and 17MB.


We can add, view or edit comments in a zip file using zipnote command,
We view comments as follows,

zipnote files.zip

The default text '@ (comment above this line)' indicates that there an no comments.

To add a comment we first redirect the output of the previous command to a file comments.

zipnote files.zip > comments

Then we edit comments file,

vim comments

Add some comments as follows,

@ file1.txt
This file contains mp3
@ (comment above this line)
@ file2.txt
Files for urls
@ (comment above this line)
@ file3.txt
File for downloads
@ (comment above this line)
@ file4.txt
This contains documents
@ (comment above this line)
@ file5.txt
@ (comment above this line)
@ (zip file comment below this line)
The general comment for this file

Then write the comments to the zip file using -w option as follows,

zipnote -w files.zip < comments

Verify that the comments have been written as follows,

zipnote files.zip

If faced with errors, you can try upgrading zip to zip 3.1 beta version.

Unzipping (Decompressing).

This involves extracting files from a zipped file.
the syntax is as follows,

unzip OPTIONS zipFile

To unzip files.zip write,

unzip files.zip

Note that if this file was previously password protected, a prompt will be displayed for us to enter that password.

Although not recommended we can avoid the prompt by using the -P option as follows,

unzip -P 0000 files.zip

Where 0000 is the password we used to compress files.zip.
This is not safe because the password can be viewed by using the history command.

We can also unzip and send the files to a different directory by using the -d option as follows,

unzip files.zip -d DIR2/

Where DIR2 is the different directory.

To decompress multiple zip files we can pass them as arguments or use a wild card character * followed by a common string.

unzip files.zip documents.zip downloads.zip


unzip '*.zip'

We can exclude a file during decompression by using the -x option as follows,

unzip files.zip -x file3.txt

Now, file3.txt will not be extracted from the zip file files.zip.

If you have followed all examples given here you will notice that during unzipping a file, a prompt,

replace file1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:

Is shown we can avoid the by using -o option which will overwrite all files.

unzip -o files.zip

Before we unzip a file we may want to check if there are any errors, for this we use the -t option,

unzip -t files.zip

If the files are valid you should see output such as the following,

testing: file2.txt                OK
testing: file3.txt                OK

Using -v option with unzip command will display details of the zip file but will not extract the files,

unzip -v files.zip


Compression and decompression can also be done using the GUI.
Compressing files is very useful for example in storing backups, sending files over a network with low bandwidth.


  1. Execute man zip or man unzip for zip or unzip manual pages.
  2. Execute zip --help or unzip --help for help.
zip command in Linux
Share this