Open In App

ZIP command in Linux with examples

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It’s among the most used compression utilities, particularly when sharing large files via email or storing them in the cloud. Below are some common reasons individuals utilize the zip command:

  • To reduce file size and save storage.
  • To combine multiple files/folders into one for easier sharing.
  • To protect files with passwords (using options).
  • To exclude or include specific file types.
  • It’s supported on almost every platform, so zipped files can be opened easily on Windows, Linux, or macOS.

Syntax:

zip [options] archive_name.zip file1 file2 folder/
  • zip: The command.
  • archive_name.zip: The name of the zip file to create.
  • file1 file2: Files or folders you want to compress.

Examples of ‘Zip’ command in Linux

Below are various examples which demonstrate how we use different zip command options in Linux:

1)  `unzip` command in ‘Zip’

unzip will list, test, or extract files from a ZIP archive. 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 (Delete a File from Archive)

The -d option allows the removal of a specific file from a zip archive without extracting it.

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 (Update Archive with New Files)

The -u option updates an existing zip archive by adding new files or replacing older versions if modifications exist.

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 (Move Files into the Archive)

The -m option moves specified files into a zip archive while deleting them from their original location.

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 (Recursively Zip a Directory)

The -r option compresses an entire directory and its contents recursively into a single archive.

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 (Exclude Files from Archive)

The -x option excludes specific files from being included in a zip archive during compression.

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 (Verbose Mode)

The -v option enables verbose mode, displaying detailed information about files and compression

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

Zip Command Options in Linux

Zip offers various options to customize compression, such as updating, excluding, or moving files. Below is a table highlighting essential zip command options in

Option What It Does Example Command
-d Delete a file from zip: Removes specific files from an existing zip archive. zip -d myfiles.zip notes.txt
-u Update the zip file: Replaces or adds files only if they’ve been modified. zip -u myfiles.zip report.txt
-m Move files into zip: Adds files to the zip and deletes them from your system. Use carefully. zip -m myfiles.zip backup.txt
-r Zip folders: Compresses an entire folder and all its contents. zip -r myproject.zip myfolder/
-x Exclude files: Leaves out certain files or folders when zipping. zip -r myfiles.zip * -x *.log
-v Verbose mode: Shows detailed progress while compressing. zip -v myfiles.zip sample.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.



Next Article

Similar Reads