Linux logo

Why using zip in Linux?

Introduction to the Zip Command in Linux

In the area of Linux command-line utilities, the zip command stands as a versatile and indispensable tool for file compression, packaging, and management. This powerful utility, prevalent across Unix-like systems, provides users with the ability to not only reduce file sizes but also bundle multiple files into a single compressed archive.

This tutorial is designed to be your guide into the world of the zip command, unveiling its capabilities, syntax, and applications. Whether you’re a seasoned Linux user or just starting your journey, understanding how to effectively use the zip command can significantly enhance your file-handling skills.

General syntax of using the zip command

Following is the general syntax of zip command:

$ zip OPTIONS Zip_File_Name FILES

An example of creating a zip file

To create a .zip (packaged and compressed) file from the command line, you can run a similar command like the one below, The -r flag enables recursive reading of files directory structure (explained more in coming section).

$ zip -r tecmint_files.zip tecmint_files
How to create a password protected zip file?

If you have sensitive information that needs to be stored in the archive you can encrypt it using the -e option:

zip -e  archivename.zip directory_name

You will be prompted to enter and verify the archive password:

Enter password:

Verify password:

What if zip utility is not installed?

The zip utility is not installed by default in most Linux distributions, but you can easily install it using the package manager of your distribution.

How to install Zip in Debian and Ubuntu

$ sudo apt install zip

Install Zip on CentOS and Fedora

sudo yum install zip
Using the -r option

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 filename.zip directory_name

Where directory_name is the folder which contains your files. The zip command in Linux thus formed will have the same directory structure along with the files. Note that when extracting the zip file, it will save all extracted files in one subdirectory.

Further Reading:  A Little About Vim and How to Search in it
Zip multiple files example

To zip one or more files, specify the files you want to add to the archive separated by space as shown below:

$ zip archivename.zip file1 file2 file3

By default the zip command prints the names of the files added to the archive and the compression method.

When specifying the Zip archive name if you omit the .zip extension it will be added automatically unless the archive name contains a dot. zip archivename.zip filename will create an archive with the same name as would zip archivename filename.

Creating Split Zip File

Imagine you want to store the Zip archive on a file hosting service that has a file size upload limit of 1GB and your Zip archive is 5GB.

You can create a new split Zip file using the -s option followed by a specified size. The multiplier can be

  • k (kilobytes)
  • m (megabytes)
  • g (gigabytes)
  • t (terabytes)

A command example:

zip -s 1g -r archivename.zip directory_name
How to unzip a zipped file?

Let us say we have a zip file name test_files.zip, you can run the unzip command as follows.

$ unzip test_files.zip
Extract Zip File to a Specific or Different Directory

To extract/unzip .zip archive files to a specific or different directory from the command line, include the -d unzip command option as shown below. We will use the same example above to demonstrate this.

This will extract the .zip file content into the /tmp directory:

$ mkdir -p /tmp/unziped

$ unzip test_files.zip -d /tmp/unziped

$ ls -l /tmp/unziped/
An example of -x option

The –x option is used to 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 –x filename.zip excluded_file

For example:

$zip –x myfile.zip hello.txt

All files will be compressed except for the hello.txt

Best practices for using zip command in Linux

  1. Understand the Purpose: Clearly understand why you are using the zip command. Whether it’s for compression, packaging files, or both, having a clear objective helps in choosing the right options.
  2. Follow Syntax Guidelines: Adhere to the general syntax of the zip command: $ zip OPTIONS Zip_File_Name FILES. This ensures a consistent and error-free usage.
  3. Recursive Zipping with -r: When dealing with directories and subdirectories, use the -r option to recursively zip files. This maintains the directory structure within the zip file.
  4. Password Protection: If dealing with sensitive information, utilize the -e option to create a password-protected zip file. Ensure strong and secure passwords for added protection.
  5. Check for zip Installation: Before using the zip command, check if it’s installed. If not, install it using the package manager of your Linux distribution to avoid any command not found issues.
    • Debian/Ubuntu:
      $ sudo apt install zip
    • CentOS/Fedora:
      $ sudo yum install zip
  6. Exclude Unwanted Files with -x: When zipping files, use the -x option to exclude specific files from being compressed. This is useful when you want to create an archive without certain files.
    $ zip -x filename.zip excluded_file
  7. Create Split Zip Files: When dealing with file size limitations, use the -s option to create split zip files. This is beneficial for scenarios where you need to upload large archives to platforms with size restrictions.
    $ zip -s 1g -r archivename.zip directory_name
  8. Consistent Naming Conventions: Maintain a consistent approach to naming zip files. Clearly indicate the purpose or content to ensure easy identification later.
  9. Document Compression Ratios: Keep track of compression ratios when compressing files. Documenting this information can be helpful in optimizing future compression tasks.
  10. Regularly Test Unzipping: Periodically test the unzipping process to ensure that your zip files are created correctly and can be successfully extracted. This helps avoid issues when sharing or archiving important files.
Further Reading:  What is SCP Command in Linux?

Common Q/A about using zip in Linux

  1. Why use zip in Linux?
    • ZIP is a compression and file packaging utility for Unix.
    • It compresses files to reduce size and serves as a file packaging tool.
    • ZIP is available on Unix-like systems, including Linux and Windows.
  2. How does zip benefit file management?
    • Compresses files to reduce size.
    • Bundles multiple files into a single archive, facilitating tasks like email attachments.
  3. What is the general syntax for the zip command?
    • The syntax is: $ zip OPTIONS Zip_File_Name FILES.
  4. How to create a password-protected zip file?
    • Encrypt with the -e option: zip -e archivename.zip directory_name.
    • Enter and verify the archive password when prompted.
  5. What if the zip utility is not installed?
    • Install it using the package manager:
      • Debian/Ubuntu: $ sudo apt install zip.
      • CentOS/Fedora: $ sudo yum install zip.
  6. How to use the -r option for recursive zipping?
    • Zip a directory recursively: $ zip –r filename.zip directory_name.
    • Maintains the directory structure within the zip file.
  7. How to zip multiple files together?
    • Specify files separated by space: $ zip archivename.zip file1 file2 file3.
  8. How to create a split zip file?
    • Use the -s option with a specified size multiplier (k, m, g, t): zip -s 1g -r archivename.zip directory_name.
  9. How to unzip a file using the unzip command?
    • Example: $ unzip test_files.zip.
  10. How to extract to a specific directory with the -d option?
    • Example: $ mkdir -p /tmp/unziped; unzip test_files.zip -d /tmp/unziped.
  11. How to exclude files using the -x option?
    • Example: $ zip –x filename.zip excluded_file.
    • Excludes specified files when creating the zip archive.
Further Reading:  What is date Command in Linux?

Conclusion

In conclusion, mastering the zip command in Linux opens up a world of efficient file management, compression, and archiving possibilities. Whether you’re reducing file sizes, creating password-protected archives, or bundling files for streamlined sharing, the zip command proves to be a versatile and powerful tool.This tutorial has walked you through the fundamentals, syntax, and various applications of the zip command. From basic usage to advanced features like recursive zipping, password protection, and creating split archives, you now have a comprehensive understanding of how to leverage this utility effectively.

As you continue to explore and experiment with the zip command in your Linux environment, you’ll discover its usefulness in various scenarios. Remember, the ability to compress, package, and organize files is a valuable skill for any Linux user, and the zip command provides you with the means to do so with ease and efficiency. Happy zipping!