Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

ZIP command in Linux with examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip {.zip-filename} file with the extension .zip.

  • Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.
  • If you have limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer them.
  • The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). An entire directory structure can be packed into a zip archive with a single command.
  • Compression ratios of 2:1 to 3:1 is common for text files. zip has one compression method (deflation) and can also store files without compression. zip automatically chooses the better of the two for each file to be compressed. The program is useful for packaging a set of files for distribution, archiving files, and for saving disk space by temporarily compressing unused files or directories.

Syntax:

zip [options] [file_name.zip] [files_names]

The syntax for Creating a zip file:

zip [file_name.zip] [file_name]

Options in ‘Zip’ command in Linux

option                                                                                                         

Description

Syntax Example
`-d`

 Removes the file from the zip archive.  After creating a zip file, you can remove a file from the archive using the -d option. 

zip -d [file_name.zip] [files_name]
zip -d myfile.zip hello7.c
`-u`

Updates the file in the zip archive. This option can be used to update the specified list of files or add new files to the existing zip file. Update an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive.

zip -u [file_name.zip] [files_name]
zip -u myfile.zip jayesh_gfg.c
`-m`

 Move the specified files into the zip archive actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space but is potentially dangerous removing all input files. 

zip -m [file_name.zip] [files_name]
zip -m myfile.zip *.c
`-r`

 To zip a directory recursively, use the -r option with the zip command and it will recursively zip the files in a directory. This option helps you to zip all the files present in the specified directory.

zip -r [file_name.zip] [directory_name]
zip -r myfile.1 jkj.gfg
`-x`

Exclude the files in creating the zip. Let’s say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option. 

zip -r [file_name.zip] -x [directory_name]
zip -r myfile.zip . -x  a.txt
`-v`

Verbose mode or print diagnostic version info. Normally, when applied to real operations, this option enables the display of a progress indicator during compression and requests verbose diagnostic info about zip file structure oddities. When -v is the only command line argument, and either stdin or stdout is not redirected to a file, a diagnostic screen is printed. In addition to the help screen header with program name, version, and release date, some pointers to the Info-ZIP home and distribution sites are given. Then, it shows information about the target environment (compiler type and version, OS version, compilation date and the enabled optional features used to create the zip executable. 

zip -v [file_name.zip] [file_name]
zip -v myfile.zip *.c

Examples of ‘Zip’ command in Linux

1)  `unzip` command in ‘Zip’

unzip will list, test, or extract files from a ZIP archive, commonly found on Unix systems. The default behavior (with no options) is to extract into the current directory (and sub-directories below it) all files from the specified ZIP archive. 

Syntax:

unzip [file_name.zip] 

Example:

Suppose we have a zip file “name = jayesh_gfg.zip” and we have three text files inside it “name = a.txt, b.txt and c.txt”. we have to unzip it in the current directory.
 

Syntax and Output:

unzip jayesh_gfg.zip

Here, we used `ls` command to display all the files that has be unzipped from the zipped file.

Unzip a file

Unzip a file

2) `-d` Option in Zip command

Syntax:

zip -d [file_name.zip] [files_name]

Suppose we have zip file “name = myfile.zip” and have eight files in it “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello7.c, hello8.c “.

We have to delete hello7.c, then…

Syntax and Output:

zip -d myfile.zip hello7.c

Here,

  1. First, we have deleted `hello7.c` successfully.
  2. Then we used “sudo unzip myfile.zip” to unzip the file for confirming that our file is deleted.
  3. Then we used “ls” to see the file that had been unzipped.

Note: Use `sudo` is you see permission denied error.
 

delete a file from zip file

delete a file from zip file

3) `-u` option in Zip command

Syntax:

zip -u [file_name.zip] [files_name]

Suppose we have zip file “name= myfile.zip” and we have to add a new file “name = hello9.c” in it.

Syntax and Output:

zip -u myfile.zip hello9.c

Here,

we have used `vi` to see that our file is added successfully.

add a file in zip file

add a file in zip file

4) `-m` option in Zip command

Syntax:

zip -m [file_name.zip] [files_name]

Suppose we have zip file “name= myfile.zip” and we have to move files “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello8.c, hello9.c ” Present in current directory to zip file.

Syntax and Output:

zip -m myfile.zip *.c

Here,

we have used `ls` to see that our files are moved successfully.

To check files inside “myfile.zip” we can type “vi myfile.zip”.

moved files inside zip file

moved files inside zip file

5) `-r` option in Zip command

Syntax:

zip -r [file_name.zip] [directory_name]

Suppose we have zip file “name= myfile.zip” and we have to move files “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello7.c, hello8.c ” present in directory “name= jkj_gfg” to zip file recursively. 

Syntax and Output:

zip -r myfile.zip jkj_gfg/ 

Here,

To check files inside “myfile.zip” we can type “vi myfile.zip”.

copy file recursively form a directory to a zip file

copy file recursively form a directory to a zip file

6) `-x` option in Zip command

Syntax:

zip -r [file_name.zip] -x [directory_name]

Suppose we have zip file “name= myfile.zip” and we have to move files “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello7.c, hello8.c ” present in directory “name= jkj_gfg” to zip file recursively. 

Syntax and Output:

zip -r myfile.zip . -x  a.txt

Here,

Here, the -r option is used to recursively add all files and directories in the current directory to the archive, and the. specifies the current directory as the source directory. The -x a.txt option excludes the file “a.txt” from the archive.

To check files inside “myfile.zip” we can type “vi myfile.zip”.

file copied recursively except one file we mentioned

file copied recursively except one file we mentioned.

7) `-v` options in Zip command

Syntax:

zip -v [file_name.zip] [file_name]

If we want to know about all the files with extension “.c”

Syntax and Output:

zip -v myfile.zip *.c
checking information about all files inside zip

checking information about all files inside zip

Most Frequently asked Question about `zip` command in Linux

1) How do I create a zip file in Linux?

By using `zip` command itself.

Example:

We are creating a zip file named = `gfg.zip` containing the files `first.txt` and `second.txt`:

Syntax:

zip gfg.zip first.txt second.txt

2) How do I extract a zip file in linux?

By using `unzip` command.

Example:

we are extracting file name = `gfg.zip`:

unzip gfg.zip

3) How do i exclude specific files from a zip archive?

By using `-x` option in zip command.

Example:

To exclude specific files from a zip archive, we can use the -x option followed by the name of the file you want to exclude. For example, the following command creates a zip file named `gfg.zip` containing all files in the current directory except for `third.txt`:

zip -r gfg.zip . -x third.txt

4) How do I include hidden files in a zip archive?

By just using `-r` option, which is copying every file recursively. For example, our zip file name is ‘gfg.zip` and we want to archive hidden files of the current directory.

zip -r gfg.zip .

5) How do I password-protect a zip archive?

By using option `-P` followed by the password we want to use. For example, our zip file name = `gfg.zip` which contain file name = `first.txt` and `second.txt`. And we also used `-e` to encrypt our zip archive, so whenever a user extracts the archive file, a prompt will come to enter the password.

zip -e -P [my_password] gfg.zip first.txt second.txt

Conclusion

Zip command in Linux is used to compress files and packaging them into a single .zip archive, which overall helps us in saving disk space and making it easy to handle big data. We have discussed various options used in zip command like -d, -u, -m, -r, -x, and -v. Overall, it is a recommended tool for Linux users to efficiently manage their files.


My Personal Notes arrow_drop_up
Last Updated : 26 Apr, 2023
Like Article
Save Article
Similar Reads