Signup/Sign In

Linux Zip Command with Examples

The Zip Command enables swift and efficient compression and decompression of files. It can help you reduce the size of your files and free up valuable storage space on your device.

If you're in a hurry. Here is a commonly used to compress all specified files, directories, and subdirectories in one go.

zip -r archive.zip file1name file2name directoryname ...

The above command with create a zip archive (archive.zip) containing all files, directories, and subdirectories specified as arguments.

In this article, we'll take a deep dive into the zip command. We'll explore how it operates, its features, and common use examples.

Syntax of Zip Command

The syntax of zip command is straightforward and easy to remember. Let's take a look.

zip [options] zipfilename [files_list]

Features of zip

Zip compression has several features, some of them are:

  1. Zip is supported by all major operating systems, including Unix (Mac), Linux, and Windows.
  2. A single command can compress an entire directory (or multiple) structure into a zip archive.
  3. Text files often have compression ratios of 2:1 to 3:1.
  4. Most familiar compression method.

Command to Unzip File on Linux

We can use unzip command for this purpose. unzip can extract, test, and list files from a ZIP archive. The default behavior is to extract each file from the ZIP archive into the current directory.

Syntax for unzipping file:

unzip myfile.zip

This command will unzip all files and directories from myfile.zip.

Check out this tutorial to dive deep into how to unzip files on Linux.

Advanced Zip Commands with examples

Let's discuss more options provided in zip tool with suitable example.

1. zip –d option

The -d removes the file or directory from the specified zip archive. For example:

Assume we have three files (named file1, file2 and file3) compressed using zip in myfiles.zip and we want to remove file2 from the archive.

zip -d myfiles.zip file2

This command will update myfiles.zip and remove file2 from it.

2. zip –u option

This option lets us add more files to the existing archive. For example:

To add file2 again in the above scenario from the current directory, we use:

zip -u myfiles.zip file2

It will again add file2 in the myfiles.zip archive.

3. zip –m option

This option is used to delete original files that are zipped. If you use it with -T, there will be no removals until zip has built the archive without any errors.

If a directory becomes empty after the files are gone, the directory is also deleted. This is good for saving disc space, but erasing all input files is possibly risky and to be used with caution.

Syntax:

zip –m myfiles.zip file4

This command will archive file4 into myfiles.zip and delete file4.

4. zip –r option

This option allows you to compress all files in the chosen directory. To recursively zip a directory, use:

$zip –r mydir.zip directory_name

This will archive all files from the directory.

5. zip –x option

When using -r flag, if you want to exclude some files from compression. You can use -x and the file names just after the command as follows:

zip –r mydir.zip directory_name -x file0

It will compress all files from the directory except the file0 file.

FAQs

Let's take a look at some questions related to zip command and file compression on Linux.

Q. Can I add a password to the archive using zip command?

Yes, zip command has an option to set a password using “-P” or “--password” flag. Here's an example:

zip -r -P your_password archive.zip files_directory

This command with create an encrypted zip file which has all files from files_directory protected by password.

Q. How to list contents of a zip archive without extracting it?

You can use the unzip command with -l options to view file and directory names archived in the zip file. For example:

unzip -l archive.zip

This command with show all files present in archive.zip as output.

Q. What other tools or methods used to compress files and directories on Linux?

There are several other tools used for compressing files and directories such as gzip tool, bzip tool, 7z, etc. All these use different compression algorithms, so there is significant difference in their efficiency.

Bzip also provides command line tools to decompress bzip archive or output bzip to terminal.

Q. What compression algorithm zip command uses?

The “zip” uses ZIP compression algorithm, which is a lossless compression method. A ZIP archives can contain multiple files and directories, preserving the original file structure. It is most commonly used in Windows operating system.

Lastly, with the correct options and arguments, you can personalize the compression level, file permissions, and other parameters to meet your specific requirements. Happy compression!



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.