Home Icon Home Computer Science Linux Zip Command: Examples, Compression & Zip File Utility

Linux Zip Command: Examples, Compression & Zip File Utility

The zip command in Linux is a powerful tool that compresses, archives, stores and manages different file types. Read on for more information on zip.
Shivangi Vatsal
Schedule Icon 0 min read
Linux Zip Command: Examples, Compression & Zip File Utility
Schedule Icon 0 min read

Table of content: 

  • Usage of Zip files
  • Advantages of Zip files
  • Installing ZIP on Linux
  • Setting the level of compression to the Zip files
expand

Linux is an open-source operating system that is widely used by developers and system administrators. One of the most useful commands in Linux is the zip command, which allows you to create and manage compressed archives. A zip command is a powerful file packaging utility that can be used to compress and extract files and directories, as well as to add, update, and delete files from a zip archive. It also supports encryption and can be used to list the contents of an archive. In this article, we will explore the various zip commands available in Linux, their syntax, and their usage.

The working of zip files is very simple. Zip files encode the given information into fewer bites by removing data that is no longer needed. This compression of data is done in such a manner that all the original data and original files are intact. We could also pass an absolute path that provides the full path to the target location.

Syntax: zip [options] zipfile files_list

Syntax to create any type of zip file: $zip myfile.zip filename.txt

Usage of Zip files

One of the simplest use cases of the zip command is to create an archive of some files present in the parent directory. ZIP is a highly useful archive file format that helps in the storage and management of different file types including tar files.

A list of files can be packaged and sent using zip; it can also be used to archive data and temporarily compress unwanted unused files or parent directories to free up disk space.

The zip application compiles one or more compressed files and information about the input file types into a single file or zip bundle (name, path, date, time of last modification, protection, and check information to verify file integrity). A single command can be used to zip up an entire directory structure.

Text files generally have compression ratios of 2:1 to 3:1. Zip may store files without compression and only has one compression mechanism i.e. deflation. However, zip can also store files without compression. When the bzip2 compression method is chosen, deflation is no longer the default method. For each file to be compressed, zip automatically selects the best option available.

When updating an existing zip file structure, the zip will first write a temporary file with the updated contents; the old unwanted file won't be overwritten until the new version has been successfully created and tested. You can also make a password-protected zip file.

The extension.zip is added if the zip archive's name does not already do so. In cases when the name already has an extension other than .zip, the current extension is retained. Split zip files, on the other hand, need the .zip extension on the final split (archives split over multiple files).

Advantages of Zip files

The zip program is used to pack and compress files. The zip command in Linux has several advantages as follows:

  • Zip files save a lot of storage space by removing unwanted files and therefore increase the efficiency of the computer by allowing the user to compress several files into a single zip archive in the current working directory.
  • The zip file format will always encrypt the user’s data. It will maintain privacy when sending files or information over the internet.
  • The zip command provides options to exclude specific files or directories from an archive, which can help reduce the size of the archive and avoid including unnecessary files.
  • The reduced file sizes of the file types also increase the speed of transfer of several files.
  • The zip command supports multiple compression levels, which allows you to balance the trade-off between compression ratio and compression speed.
  • The zip command can be easily automated using shell scripts, which can help streamline repetitive tasks and make it easier to perform backups and other system administration tasks.

Installing ZIP on Linux

The zip and unzip utilities are already pre-installed in newer Linux directory paths like Ubuntu 20.04 and CentOS 8. But if it is not installed on your systems, you can run the following commands to install Zip in the current directory.

Installing Zip:

Step 1: Open the Linux terminal emulator (Launch the Terminal using the Ctrl+Alt+T keyboard).

Step 2:Type "cd" to change the current directory.

Step 3: Install the zip utility by running the command.

$ sudo apt install zip

Step 4: After installation, you can confirm the version of the zip installed using the command.

$ zip -v

Linux Zip command options

The zip command supports many options, which allow users to customize the compression level, file inclusion/exclusion, and other aspects of the archive creation process. Some of the most commonly used zip option names in Linux along with their option descriptions are discussed below: 

1) -u Option

This option updates the existing file in the zip archive. It also adds new files or directories to the archive and updates existing files with new versions if available.

Command: $zip –u file_name.zip file.txt

2) -d Option

This option deletes the file from the zip archive. It also deletes the created zip file.

Command: $zip –d file_name.zip file.txt

3) -m Option

This option will delete the original/main files after zipping. It will move the individual files by making a zip and deleting the original files/folder.

Command: $zip –m file_name.zip file.txt

4) -x Option

This option will exclude files that are not needed when we are going to create the zip.

Command: $zip –x file_name.zip file_to_be_excluded

5) -r (recursive) Option

The -r option will recursively zip the files inside it and then the folders inside it. Patterns are relative in standard file paths as they appear in the archive, or will after zipping, and can have optional wildcards in them.

Command: $zip –r file_name.zip directory_name

6) -v Option

When applied to the real operation, this option will display the progress indicator during compression and request verbose diagnostic info about the zip structure when applied to real operations. When -v (requests verbose) is the only command line argument, and either stdin or stdout is not redirected to a file, a diagnostic screen header 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.).

Command: $zip –v file_name.zip file.txt

7) -q Option

If you do not want to display the standard output of the zip command, use the -q (quiet) option with the zip command. Similar to the zip command, you can also hide the output archive of the unzip command using the -q option. To use the -q option in the zip/unzip command, run the following :

Command: $ zip -q test.zip testfile.txt

8) -z option

The "-z" option in the Linux zip command is used to compress the current files added to the archive into a single zip file. This option specifies that the files should be compressed using the gzip compression algorithm.

To use the following option in your desktop environment, run the following:

Command: zip -z archive_name file1 file2 file3

9) -e option

The "-e" option in the Linux zip command is used to encrypt the current files added to the archive zip program. This option specifies that the files should be encrypted using a password or passphrase.

Run the following command in the terminal: 

Command: zip -e archive_name file1 file2 file3

10) -l option

This option translates Unix end-of-line (LF) characters into MS-DOS/Windows CR LF characters. It ensures compatibility when extracting files on DOS-based systems. It should only be used on text files; binary files should be excluded to avoid corruption. If files already contain CR LF, this option adds an extra CR, so use it with care. This ensures that unzip -a on Unix systems can reverse the effect and recover the original LF-only line endings.

Command: $zip -l file_name.zip file.txt

11) -o option

The "-o" option in the Linux "zip" command is used to overwrite existing files in the ZIP archive without prompting the user for confirmation. When this option is used along with the "zip" command, it will create a new ZIP archive or update an existing one with the specified files. Any existing files with the same name will be overwritten.

Command: zip -o archive.zip file1.txt file2.txt

12) -j option 

The -j option in the Linux zip command stores files with the given names only. The j option doesn't store directory information.

Command: zip -j myarchive.zip directory1/file1.txt directory1/file2.txt

13) -x and -i options

In the Linux zip command, the -x and -i options are used to exclude or include certain files or directories in the archive creation process. The -x option allows you to exclude files or source directories from the archive. For example, if you want to create an archive of all files and directories in the current directory, except for any files with a .log extension, you can use the following command:

zip -r myarchive.zip . -x "*.log"

The -i option is used to include only certain files or directories in the archive. For example, if you want to create an archive of only the files and directories in the current directory that start with the letter "a", you can use the following command:

zip -r myarchive.zip . -i "a*"

14) Split files with the zipsplit command

The "zipsplit" command allows you to split a ZIP archive into multiple files of specified sizes or numbers of files in the current directory.

Command: zipsplit [options] filename

There are times when the zip original file size is so large that we cannot upload it or send it as an attachment due to original file size limitations. We can divide and split files into smaller files that are simpler to transfer with the zipsplit command.

To split files to the target directories, run the following command syntax in

$ zipsplit –n <size> zip_file

15) -Sp option

There is no -sp option available in the zip command in Linux. However, the -sp option is used in some third-party utilities that provide a wrapper around the zip command. The Linux version of the zip command itself does not have a -sp option. Instead, you can use the -P option followed by the password to set a password for the archive. For example:

zip -r -e -P mypassword myarchive.zip mydir/

Setting the level of compression to the Zip files

The amount of compression that will be applied to the files being zipped in the current directory can also be adjusted. Compression levels range from 0 to 9, with 6 serving as the default value, 0 as the lowest value (slowest compression speed), and 9 as the greatest number (fastest compression speed). The default compression level is 6.

For example, if you want to zip all text files to files1.zip using the highest optimal compression value, the command would be:

$ zip -9 files1.zip *.txt

Similarly, if you want to zip all text files to files2.zip using the lowest compression value -0, the command would be:

$ zip -0 files2.zip *.txt

The levels of compression available in the general target environment have the following effects when creating ZIP files: Maximum - The slowest compression method, but the best for making small archives. Normal: The default mode. Low - Faster yet less effective than the default.

Conclusion

Linux zip command is useful for packaging a set of files for distribution. This can be useful for saving disk space, transferring files between systems, and simplifying backup processes. By default, versions of zip and unzip packages are pre-installed in Ubuntu systems. It is analogous to a combination of the Unix tar command and compresses, and it is compatible with PKZIP. Just like zip, the unzip tool has a lot of options that are useful. Overall, the zip command in Linux provides a flexible and powerful tool for creating and managing compressed archives.

Suggested reads:

Edited by
Shivangi Vatsal
Sr. Associate Content Strategist @Unstop

I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.

Tags:
Computer Science

Comments

Add comment
comment No comments added Add comment