Home » Linux Commands » How to Zip and UnZip Files in Linux

How to Zip and UnZip Files in Linux

Compression utilities compress files (borg and rar) and are similar in their functions and operations. Zip/unzip is one of the commands used in Linux to create compressed files and to extract. In modern Linux distros these utilities are installed by default. If any issues, you can install both these utilities from the shell prompt but you must be a root user to do so.

On RedHat/CentOS based system

# yum install zip unzip

On Ubuntu/Debian based system

# apt-get install zip unzip
  • Zip is a compression and file packaging utility for Linux and Unix.
  • Unzip will list, test, or extract files from ZIP archive files.

The zip and unzip programs work exactly in the same way as PKZIP and PKUNZIP in the DOS environment. Compressed files use less disk space than normal files and they transfer faster than larger uncompressed files. In Linux, you can compress files with the help of Gzip or Zip. If you exchange files with non-Linux users then you can use zip to avoid compatibility problems.

Zip file (Compress)

The compression process replaces the original file with a new file that has an additional extension which is .zip (the case of zip command), so don’t delete the .zip  files that you create. They are the original files in a compressed wrapper! The syntax is in the form

# zip [option] [-b path] filezip file
  • option: there are available options of zip command
  • -b path: where to save temporally the zip file until the compression finish and then copy it in the current working directory
  • filezip: it is the name of zip file which will be created
  • file: it is the name of the original file. It can be more than one file

The command given below creates a file file.zip which contains a copy of the file named  file10 located in the current directory.

# zip compressfile file10
# ls -l file*
-rw-r--r-- 1 root root 17 May 17 20:44 file1
-rw-r--r-- 1 root root 179 May 17 20:44 compressfile.zip

To compress a folder, we used -r option of zip command. The command given below will compress the directory script-test into the file name back.zip:

# zip -r back script-test/
 adding: script-test/ (stored 0%)
 adding: script-test/hello2 (deflated 8%)
 adding: script-test/password-gen.sh (deflated 4%)
 adding: script-test/continue.sh (deflated 20%)
 adding: script-test/folder1/ (stored 0%)
 adding: script-test/folder1/file1 (stored 0%)

See how it operates on the file's directory too. You can see the result

# ls -l back.zip 
-rw-r--r-- 1 root root 6255 May 21 12:31 back.zip

You can add the .zip extension at the end of the zip file yourself.

# zip extension.zip file10 
 adding: file10 (stored 0%)

The command will directly understand without adding the same extension again.

# ls -l extension.zip 
-rw-r--r-- 1 root root 179 May 21 13:15 extension.zip

You can see the result.

Unzip file (Extract)

Unzip command extract the .zip file only. So we will use it to extract the zip files created. To extract a file do as below

# unzip compressfile.zip 
Archive: compressfile.zip
 extracting: file10

It extracts the content in the current directory. So, if there are the originals files, it will ask if you want to replace it

# unzip file.zip
replace file10? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
 extracting: file10

When the zip file contains more than one file, it will do the operation with all the files. Let's try to extract the zip folder

# unzip back.zip 
Archive: back.zip
  creating: script-test/
 inflating: script-test/hello2 
 inflating: script-test/password-gen.sh 
 inflating: script-test/continue.sh 
  creating: script-test/folder1/
extracting: script-test/folder1/file1

More Zip and Unzip Command Options

1) Compressing the current directory

The command given below will compress the current directory and also all sub-directories:

# zip -r backupfile.zip .
 adding: hello2 (deflated 8%)
 adding: script-test/ (stored 0%)
 adding: script-test/hello2 (deflated 8%)

2) Compressing all the file in current directory

The command written below will create a file called backfile.zip which contains all files in the current directory. File names that start with a "." are not included.

# zip backfile.zip *
 adding: administration.sh (deflated 26%)
 adding: backupfile.zip (stored 0%)
 adding: back.zip (stored 0%)
# ls -l backfile.zip 
-rw-r--r-- 1 root root 28702 May 21 13:09 backfile.zip

3) Extracting all the compressed file in the current directory

You can extract all the zip file as below

# unzip *.zip
Archive: backfile.zip
 extracting: backupfile.zip 
 extracting: back.zip 
 extracting: compressfile.zip

4) Forcefully overwrite existing files when decompressing

During the decompression, if there are originals files, unzip command will ask you if you want to replace or to rename the newly extracted files. The operation can be not easy if you have some files in the compressed archive.

# unzip script-fol.zip 
Archive: script-fol.zip
replace script-test/extension.zip? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: script-test/extension.zip 
replace script-test/hello2? [y]es, [n]o, [A]ll, [N]one, [r]ename:

So you can force the unzip command to overwrite the existing files with unzip -o command

# unzip -o script-fol.zip 
Archive: script-fol.zip
 extracting: script-test/extension.zip 
 inflating: script-test/hello2 
 extracting: script-test/script.zip 
 inflating: script-test/bonjour~ 
 inflating: script-test/script-test/hello2 
 inflating: script-test/script-test/bonjour~ 
 inflating: script-test/script-test/password-gen.sh

5) Compressing file to another directory

It is possible to zip a file and save the zip file in another directory by indicating the new destination path with the name of the zip file at the end.

# zip test/zipfolder/file10.zip file10 
adding: file10 (stored 0%)

6) Decompressing archive to another directory

As with the compression operation, it is possible to extract files into another directory. It is with -d option of unzip command

# unzip file10.zip -d ~/script-test/
Archive: file10.zip
 extracting: /root/script-test/file10

7) Best compression

You can also try -9 option for best compression as shown below:

# zip -9 -r backupfile mydata

8) Compressing multiple files

It is possible to compress multiples files into one zip file. And we compress it to another directory

# zip /mnt/zip/script.zip continue.sh hello password-gen.sh 
 adding: continue.sh (deflated 20%)
 adding: hello (deflated 8%)
 adding: password-gen.sh (deflated 4%)

9) See the content of a zip file without decompressing

It is possible to see the content of a zip file with the unzip -l command. It is like when you list a detail the content of a folder without move on it.

# unzip -l /mnt/zip/script.zip
Archive: /mnt/zip/script.zip
 Length Date Time Name
--------- ---------- ----- ----
 179 04-25-2017 02:10 continue.sh
 66 05-15-2017 20:12 hello
 107 04-25-2017 02:10 password-gen.sh
--------- -------
 352 3 files

10) Extracting one or more file from a compressed archive

If a compressed archive has more than one file, it is possible to extract only one or more files. This operation will not extract all the file of the compressed archive. You need to specify each filename to extract

# unzip /mnt/zip/script.zip hello continue.sh -d ~/test/zipfolder/
Archive: /mnt/zip/script.zip
 inflating: /root/test/zipfolder/continue.sh 
 inflating: /root/test/zipfolder/hello

Using the command above means that you already know exactly the name of the files to extract. So, it means for example that you have listed the content of the compressed archive with unzip -l command

11) Exclude certain file during the decompression

As we are able to filter file to extract, it is possible to exclude certain files during the decompression

# unzip backfile.zip -x file10 administration.sh -d /tmp
Archive: backfile.zip
 inflating: /tmp/administration_complete 
 inflating: /tmp/administration_total 
 extracting: /tmp/backupfile.zip 
 extracting: /tmp/back.zip 
 inflating: /tmp/bonjour~ 
 extracting: /tmp/compressfile.zip 
 inflating: /tmp/continue.sh 
 creating: /tmp/folder1/
 inflating: /tmp/hello 
 inflating: /tmp/hello2 
 creating: /tmp/intro-script/
 inflating: /tmp/mon-nom 
 inflating: /tmp/password-gen.sh 
 creating: /tmp/script-test/
 extracting: /tmp/toto h. Check the integrity of a compressed archive

You can check if a compressed archive has not error. It is possible with unzip -t command

# unzip -t backfile.zip 
Archive: backfile.zip
 testing: administration_complete OK
 testing: administration.sh OK
 testing: administration_total OK
 testing: backupfile.zip OK
 testing: back.zip OK
 testing: bonjour~ OK
 testing: compressfile.zip OK
 testing: continue.sh OK
 testing: file10 OK
 testing: folder1/ OK
 testing: hello OK
 testing: hello2 OK
 testing: intro-script/ OK
 testing: mon-nom OK
 testing: password-gen.sh OK
 testing: script-test/ OK
 testing: toto OK
No errors detected in compressed data of backfile.zip.

12) Decompressing an archive without creating directories

If a compressed archive contains a folder, during the extraction operation it will recreate the exact structure. It is possible to extract the archive without creating the folder which were already presents. But their contents will be extracted directly

We will consider the zip archive of the example above. It contains folder1 and script-test folders, we will extract without creating the two

# unzip -j backfile.zip 
Archive: backfile.zip
 inflating: administration_complete 
 inflating: administration.sh 
 inflating: administration_total 
 extracting: backupfile.zip 
 extracting: back.zip 
 inflating: bonjour~ 
 extracting: compressfile.zip 
 inflating: continue.sh 
 extracting: file10 
 inflating: hello 
 inflating: hello2 
 inflating: mon-nom 
 inflating: password-gen.sh 
 extracting: toto

You can see the folders are not present

13) List detailed information about the archive

It is possible to have the detail information about the content of an archive like with ls -l command.

# unzip -Z backfile.zip 
Archive:  backfile.zip
Zip file size: 28702 bytes, number of entries: 17
-rwxr-xr-x  3.0 unx      147 tx defN 17-Apr-25 02:21 administration_complete
-rwxr-xr-x  3.0 unx      144 tx defN 17-Apr-25 02:10 administration.sh
-rwxr-xr-x  3.0 unx      147 tx defN 17-Apr-25 02:19 administration_total
-rw-r--r--  3.0 unx    18727 bx stor 17-May-21 12:54 backupfile.zip
-rw-r--r--  3.0 unx     6255 bx stor 17-May-21 12:45 back.zip
-rwxr-xr-x  3.0 unx       78 tx defN 17-Apr-25 02:10 bonjour~
-rw-r--r--  3.0 unx      179 bx stor 17-May-21 12:44 compressfile.zip
-rwxr-xr-x  3.0 unx      179 tx defN 17-Apr-25 02:10 continue.sh
-rw-r--r--  3.0 unx       17 tx stor 17-May-17 20:44 file10
drwxr-xr-x  3.0 unx        0 bx stor 17-May-17 04:28 folder1/
-rwxrwxr--  3.0 unx       66 tx defN 17-May-15 20:12 hello
-rwxrwxr--  3.0 unx       66 tx defN 17-May-15 20:12 hello2
drwxrwxrwt  3.0 unx        0 bx stor 17-Apr-27 02:44 intro-script/
-rwxr-xr-x  3.0 unx      147 tx defN 17-Apr-25 02:10 mon-nom
-rwxr-xr-x  3.0 unx      107 tx defN 17-Apr-25 02:10 password-gen.sh
drw-r--r--  3.0 unx        0 bx stor 17-May-19 17:41 script-test/
-rwxr-xr-x  3.0 unx       20 tx stor 17-Apr-25 02:10 toto
17 files, 26279 bytes uncompressed, 26118 bytes compressed:  0.6%

It will give some information such as the archive name, its total size, and the total number of files in it. The trailer gives the number of files listed, the total uncompressed and compressed data size of the listed files (not counting internal zipfile headers), and the compression ratio.

14) Update one file or more files of the compressed archive

Suppose we have compressed an archive then, modified a file. There is a possibility to add the update file to the compressed archive with the zip -u command.

In the example below, we have updated a file toto and add a new file papi

# zip -u backfile.zip toto papi
 adding: toto/ (stored 0%)
 adding: papi (stored 0%)

Now let's check

# unzip -Z backfile.zip | grep "toto" 
-rwxr-xr-x  3.0 unx        0 bx stor 17-May-16 17:46 toto
# unzip -Z backfile.zip | grep "papi" 
-rw-r--r--  3.0 unx        0 bx stor 17-May-22 01:10 papi

You can the date on the output of each command. You can see that the update date is may instead of april

15) Update all the files of the compressed archive

You can replace (freshen) an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive. Unlike the update option, this will not add files that are not already in the zip archive.

# zip -f test.zip

16) Do compression and extraction quietly

When we do compression of extraction, normally zip command show the output of the operation. It is possible to decide not to show the result to the output with -q option of zip command but we will need to check the result after a certain time

# zip -rq /mnt/zip/script-fol.zip script-test/
# ls -l /mnt/zip/
total 68
-rw-r--r-- 1 root root 69072 May 22 01:35 script-fol.zip

17) Converting text files when decompressing

Some non-linux systems store text files with two end-of-line characters CR and LF rather than the more efficient single character (LF) used on all Unix systems. You can have issue compatibility. You can use unzip -a to face it with a mnemonic for ASCII conversion. It converts files that are supposedly text, while leaving alone those that are marked binary.

# unzip -a script-fol.zip 
Archive: script-fol.zip
 creating: script-test/
 extracting: script-test/extension.zip [binary]
 inflating: script-test/hello2 [text] 
 extracting: script-test/script.zip [binary]
 inflating: script-test/bonjour~ [text]

18) Compressing only sub-directory of a main directory into individual archive

Suppose that we have a folder with some files and sub-directories. Now suppose that we only need to compressed theses sub-directory. I will give you a script line to do it. Make sure you enter to the main directory first.

We will first check if we are into the correct directory

# pwd

Now let's see the sub-directories we have

# ls -lF /mnt/zip/test
total 12
-rw-r--r-- 1 root root 0 May 22 02:31 COURSE
-rw-r--r-- 1 root root 0 May 22 02:31 FILE
-rw-r--r-- 1 root root 434 May 22 02:33 file.zip
drwxr-xr-x 2 root root 4096 May 22 02:44 pap/
drwxr-xr-x 2 root root 4096 May 22 02:44 pop/
-rw-r--r-- 1 root root 0 May 22 02:31 TEXT

You can see that we have pap and pop sub-folder. Let's try our script

# for i in */; do zip -r "${i%/}.zip" "$i"; done
 adding: pap/ (stored 0%)
 adding: pop/ (stored 0%)

Let's check their zip files

# ls
COURSE FILE file.zip pap pap.zip pop pop.zip TEXT

You can see that it creates the zip folder with the respectively name. Note that our command only compress each level 1 sub-directory of the main directory not every descendant directory of the sub-directory.

If you want to see how this works, include an echo before the zip and you will see the commands printed instead of executed.

# for i in */; do echo zip -r "${i%/}.zip" "$i"; done
zip -r pap.zip pap/
zip -r pop.zip pop/

19) Compressing both files and directory into individual archive

You can compress both the files and directories into individual archive for some reason. To do it, you can use the script line below

# for i in *; do zip -r "${i%}.zip" "$i"; done
 adding: COURSE (stored 0%)
 adding: FILE (stored 0%)
 adding: file.zip (stored 0%)
 adding: pap/ (stored 0%)
 adding: pop/ (stored 0%)
 adding: TEXT (stored 0%)
# ls
COURSE.zip file.zip file.zip.zip pap.zip pop.zip TEXT.zip

20) Show detailed information about a compressed archive

You can have more detail information about a compressed archive with -v option of unzip command

# unzip -v backfile.zip 
Archive:  backfile.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
     147  Defl:N      125  15% 04-25-2017 02:21 beced85d  administration_complete
     147  Defl:N      125  15% 04-25-2017 02:19 beced85d  administration_total
   18727  Stored    18727   0% 05-21-2017 12:54 9a9d9d5b  backupfile.zip
    6255  Stored     6255   0% 05-21-2017 12:45 57a487bf  back.zip
      78  Defl:N       70  10% 04-25-2017 02:10 7ea59cec  bonjour~


In this tutorial, we have understood the compression(zip) and decompression(unzip) concept and we have seen how we can manipulate compressed files. Let us know if you know about more options.

Read Also:

1 thought on “How to Zip and UnZip Files in Linux”... add one

  1. How to save zip into path to compress multiple folders, each into its own zip archive with using this command "for i in */; do zip -r "${i%/}.zip" "$i"; done" for now its saving in main directory , kindly help!


Leave a Comment