[ZIP File/Zip Folder Linux] Zip Command in Linux with Examples

Are you looking for ZIP commands in Linux that can help you to zip files Linux and zip folder Linux? Then read our blog post to have a detailed understanding of it.

Updated: 25 Oct, 23 by Antoniy Yushkevych 11 Min

List of content you will read in this article:

The most popular lossless data compression archive file format is Zip. A Zip file is a data object that encapsulates one or more compressed files and folders. Uncompressed files take up more disc space and take longer to transmit from one device to another. Zip files may be readily extracted on Microsoft Windows, macOS, and Linux operating systems using the appropriate software for each platform. In this guide, you'll learn how to use the zip command in Linux with examples [ZIP File Linux/Zip Folder Linux] to compress files and directories.

In this section, we'll delve into a detailed explanation of the Zip command in Linux and its multifaceted role in computing. Zip isn't just a file compression tool; it's a versatile utility that serves as a compression engine and a file packaging solution.

  • Versatile Utility: Zip is a versatile tool that serves a dual purpose. It functions not only as a compression tool but also as a sophisticated file packaging utility.
  • Compression Function: At its core, Zip is designed to compress files and directories, reducing their size significantly. This compression feature is essential for efficient storage and transmission of data, particularly when dealing with large files or limited bandwidth connections. Ready to Optimize Your Website's Load Time? Try Compressing Your .jpeg and .png Images Now!
  • File Packaging: Beyond compression, Zip excels at packaging files and directories into a single .zip archive. This archive contains the compressed data and essential metadata about each encapsulated file. This metadata includes file names, paths, dates of last modification, protection settings, and even checks to verify file integrity.
  • Cross-Platform Compatibility: Zip's versatility extends to its cross-platform compatibility. It's not confined to a particular operating system; instead, it's available on a wide range of platforms, including Unix, Linux, Windows, and more. This cross-compatibility empowers users to work seamlessly with Zip archives regardless of the operating system they're using.
  • Broad Applicability: Zip's wide-reaching compatibility and functionality make it a valuable tool for various tasks. Whether you need to conserve disk space, distribute files efficiently, or streamline data transfers, Zip provides a robust and flexible solution.

Understanding the various options available with the zip command in Linux is essential for harnessing its full potential. In this article, we've dedicated a dedicated section to elucidate these options, providing both detailed descriptions and the precise syntax for each one.

  • -d (Remove files from the archive): The -d option allows you to selectively remove specific files from an existing zip archive. After creating a zip file, you can employ this option to remove files of your choice using the following syntax:

zip -d [file_name.zip] [files_name]

  • -u (Update files in the archive): With the -u option, you can seamlessly update files within an already existing zip archive. This feature is particularly handy when you need to add new files or replace existing ones. The update process takes place only if the modified version of a file is more recent than the one already residing in the zip archive. The syntax for updating files is as follows:

zip -u [file_name.zip] [files_name]

  • -m (Move files into the archive): The -m option not only adds specified files to the zip archive but also moves them into the archive. Additionally, it deletes the source files or directories after successfully creating the zip archive. If a directory becomes empty due to the removal of files, it is also deleted. However, it's important to exercise caution when using this option, as it results in the permanent removal of input files. The syntax for moving files into the archive is as follows:

zip -m [file_name.zip] [files_name]

  • -r (Recursively zip a directory): The -r option is a powerful tool for recursively zipping an entire directory and all of its files. It includes all the files present in the specified directory and its subdirectories in the resulting zip archive. This option is incredibly useful when you need to create comprehensive zip archives containing an entire directory structure. The syntax for recursively zipping a directory is as follows:

zip -r [file_name.zip] [directory_name]

  • -x (Exclude files from the zip): The -x option offers precise control over the contents of your zip archive. You can use this option to exclude specific files from being included in the archive. This proves handy when you want to zip all files in a directory but wish to exclude certain unwanted files. The syntax for excluding files from the zip archive is as follows:

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

  • -v (Verbose mode): When you need diagnostic information during compression, the -v option comes into play. It enables the verbose mode, providing a wealth of details about the zip file structure, compression progress, and the zip executable's information. You can even employ it to gain insights into your target environment. If used alone, it prints a diagnostic screen, offering comprehensive information about the zip operation. The syntax for enabling the verbose mode is as follows:

zip -v [file_name.zip] [file_name]

Learning how to use the zip command in Linux becomes much more accessible when you have real-world examples to follow. In this article, we provide several practical examples that illustrate various scenarios and use cases for the zip command in Linux. These examples are designed to help readers grasp how to effectively utilize the command in different situations. Let's explore a few of them:

  1. Deleting Files from a Zip Archive:

    • Scenario: You have a zip archive, and there are specific files within it that you want to remove.
    • Example:

zip -d [file_name.zip] [files_name]

This command allows you to selectively remove files from an existing zip archive. Replace [file_name.zip] with the name of your zip archive and [files_name] with the names of the files you wish to delete.

  1. Updating Files within a Zip Archive:

    • Scenario: You want to update files within an existing zip archive, either by adding new files or replacing existing ones.
    • Example:

zip -u [file_name.zip] [files_name]

Use this command to update files in a zip archive. If the modified version of a file is more recent than the one in the archive, the update will take place. Replace [file_name.zip] with your zip archive's name and [files_name] with the files you want to update.

  1. Moving Files into an Archive:

    • Scenario: You need to move specific files into a zip archive while also deleting the source files.
    • Example:

zip -m [file_name.zip] [files_name]

This command moves the specified files into the zip archive and deletes the source files. Be cautious when using this option, as it results in the permanent removal of the input files.

  1. Recursively Zipping a Directory:

    • Scenario: You want to create a zip archive that includes an entire directory and all its subdirectories and files.
    • Example:

zip -r [file_name.zip] [directory_name]

Use this command to recursively zip a directory and its contents. Replace [file_name.zip] with the desired name for your zip archive and [directory_name] with the name of the directory you want to zip.

To zip file/folder with the zip command in Linux, you have to  divide the files to be included in the pack by a space, as demonstrated below:

zip xyz.zip file1 file2 file3

Output:

adding: file1 (deflated 53%)

adding: file2 (stored 0%)

adding: file3 (deflated 48%)

The zip command in Linux publishes the names of the files that have been added to the archive as well as the compression technique by default. Unless the file that has to be compressed contains a .zip extension, the extension is created automatically if the archive name does not conclude with it.

zip xyz.zip file1 will produce the same archive as zip xyz file1.

  1. Use the -q option to suppress the output of the zip command in Linux:

zip -q xyz.zip file1 file2 file3

Creating a zip archive of a directory that includes the content of subdirectories is common. 

  1. You may recursively scan the full directory tree using the -r option:

zip -r xyz.zip dir1

  1. In the same zip file linux, you can also add numerous files and directories:

zip -r xyz.zip dir1 dir2 filename1 filename2

Deflate is Zip's default compression algorithm. If a file cannot be compressed, the zip utility just puts it in the file as it is, instead of compressing it with the store method. The bzip2 compression algorithm is supported by the zip program in most Linux variants.

Use the -Z option to select a compression method.

zip -r -Z bzip2 xyz.zip dir1

Output:

adding: sub_dir/ (stored 0%)

adding: sub_dir/filename1 (bzipped 27%)

adding: sub_dir/filename2 (bzipped 68%)

The zip command in Linux lets you define a compression level from 0 to 9 by providing a number preceded by a dash. The compression level is set to -6 by default. When -0 is used, all files are saved without compression. -9 forces the zip command in Linux to perform the best compression possible for all files.

For example, if you wanted to utilize compression level -9, you would type:

zip -9 -r xyz.zip dir1

The zip operation becomes more CPU-intensive when the compression level is increased, and it takes longer to complete.

If you have sensitive data that has to be saved in the archive, use the -e option to encrypt it:

zip -e  xyz.zip dir1

The archive password must be entered and verified using the following command:

Enter password:

Verify password:

The password you will be entering will not be seen on the screen. However, the next time you log in, make sure you remember your password.

Assume you wish to upload the Zip archive to a file hosting provider with a 1GB upload restriction, and your Zip archive is 5GB in size.

To create a new split Zip file, use the -s option followed by size. k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes) can be used as the multiplier.

zip -s 500m -r xyz.zip dir

After a set hits the stated size limit, the operation above will keep producing new archives in it.

The zip command in Linux may be used to build Zip archives [Zip files and Folders in Linux]. On a Linux operating system, the unzip command may also be used to decompress a ZIP file. We have seen how to zip various files and directories in this tutorial and various available options for using the zip command in Linux.

 

The Zip command in Linux is a versatile utility that serves as both a compression tool and a file packaging solution. It's used to compress files and directories, reducing their size, and it can package multiple files and folders into a single .zip archive. Zip is available on various operating systems, making it a cross-platform tool.

To zip a file in Linux, you can use the zip command in Linux followed by the archive name and the list of files to include. For example: zip archive.zip file1 file2 file3

You can send a Zip file like any other file attachment through email or file-sharing services. Simply attach the Zip file to your email or upload it to the file-sharing platform of your choice and share the link or send it to the recipient.

The Zip command works by compressing files and directories, reducing their size to save storage space and facilitate faster data transfer. It creates a single .zip archive that contains the compressed data and metadata about each file. Zip offers various options, including compression methods, file removal, updates, and more, to provide flexibility in managing files and archives.

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com