Guide to using Zip and Unzip commands in Linux

Step-by-Step Guide Zip Unzip Linux ubuntu debian fedora centos redhat

Zip and unzip functions make file transfer tasks much simpler. This tutorial will show you how to use Linux commands to unzip files and improve your VPS workflow.

Zip is a commonly used compression function that is portable and user-friendly. You can even unzip files in Windows that were created in Linux.

Unzip is not included by default in most Linux flavors, but it can be easily installed. By creating .zip files, you can achieve the same level of compression as .tar.gz files.

What Are Zip Files Used For?

Here are some common scenarios in which you might use zip files:

  • Working frequently between Windows and Unix-based systems: Zip files not only compress files but also act as file packaging utilities, making them compatible with multiple operating systems.
  • Saving bandwidth: If you have limited or restricted bandwidth, you can use zip files to transfer files between two servers.
  • Reducing transfer time: The zip utility reduces file size, making file transfers faster.
  • Uploading or downloading directories more quickly.
  • Saving disk space.
  • Unzipping password-protected .zip files.
  • Enjoying a good compression ratio.

Installing Unzip on Debian and Ubuntu Systems

Installing unzip is simple. If you’re using Ubuntu or Debian, use the following command to install unzip:

$ sudo apt install unzip

Once you’ve initiated an installation, wait for a minute until it’s finished.

To create zip files, you’ll need to install zip. Use the following command to do so:

$ sudo apt-get install zip

Installing Unzip on Linux CentOS and Fedora

Installing unzip on Linux CentOS and Fedora is similarly straightforward. Use the following command:

$ sudo yum install unzip

After the installation is complete, you can confirm the path with the following command:

$ which unzip

Once you’ve executed the command in the terminal, you should see output similar to this:

$ /usr/bin/unzip

You can also check that the installation was successful by using the following command, which displays verbose information about the unzip utility:

$ unzip -v

Using Zip and Unzip in Linux

Now that we’ve learned how to install the utility, let’s explore some of its basic uses:

Create Zip Files in Linux

The basic syntax for creating a .zip file is:

$ zip options zipfile list_Of_files

To demonstrate this, we’ll create two files – File1.txt and File2.txt – and compress them into a file named ZipFile.zip using the following command:

$ zip ZipFile.zip File1.txt File2.txt

Unzipping Files in Linux

The unzip command can be used without any options to extract all files to the current directory. For example:

$ unzip ZipFile.zip

This command will extract the contents of ZipFile.zip to the current directory, provided that you have read-write access to the directory.

Remove a File from a .zip File

Once a .zip file is created, you can remove files from it. If you want to remove File1.txt from the existing ZipFile.zip, use the following command:

$ zip –d ZipFile.zip File1.txt

After running this command, you can extract the modified .zip file with:

$ unzip ZipFile.zip

You’ll notice that File1.txt is no longer included in the extracted files.

Using Zip in Linux to Update and Move Files

Updating a Zip File

After creating a .zip file, you can add new files to it or move specific files to it using the following commands:

$ zip –u ZipFile.zip File2.txt

Once executed, you can extract ZipFile.zip to find the newly added file.

Moving Files to a Zip File

To move specific files to a .zip file and delete them from their original directories, use the -m option in the command:

$ zip –m ZipFile.zip File2.txt

Recursive Use of Zip on Linux

The -r option allows you to recursively zip files within a directory, including sub-directories. For example, to compress all files in the directory MyDirectory, use the command:

$ zip –r ZipFile.zip MyDirectory

In the example, MyDirectory is a directory which has multiple files and sub-directories to be zipped.

Excluding Files and Directories in Zip Files

To exclude multiple files or directories while creating a .zip file, you can use the -x option followed by the list of files or directories you want to exclude. For example, to exclude File1.txt and Directory1 from the ZipFile.zip, you can use the following command:

$ zip -x File1.txt Directory1 -r ZipFile.zip .

Here, the -r option is used to recursively include all the files and directories in the current directory (.).

Unzipping Files to a Specific Directory

If you want to extract the contents of a .zip file to a specific directory, you can use the -d option followed by the path of the destination directory. For example, to extract the contents of ZipFile.zip to the directory /home/user/extracted, you can use the following command:

$ unzip ZipFile.zip -d /home/user/extracted

Extracting Multiple Zip Files

To extract multiple .zip files in the current directory, you can use the following command:

$ unzip '*.zip'

This command will extract all the individual .zip files in the current directory.

Suppressing Output When Using Unzip in Linux

To suppress the output messages when using the unzip command, you can use the -q option. For example, to extract ZipFile.zip without displaying any output messages, you can use the following command:

$ unzip -q ZipFile.zip

Excluding Files with Unzip in Linux

To exclude specific files when extracting from a .zip file, you can use the -x option followed by the file name or pattern to exclude. For example:

$ unzip ZipFile.zip -x excludedFile.txt

This will extract all files from ZipFile.zip except for excludedFile.txt.

You can also exclude files based on their file type. For example, to exclude all .png files from being extracted, you can use the following command:

$ unzip ZipFile.zip -x "*.png"

The above command will exclude all .png files from being extracted.

Extracting Password Protected Zip Files in Linux

If you need to extract a password-protected .zip file, you can use the -P option followed by the password. For example:

$ unzip -P Password ZipFile.zip

Here, Password should be replaced with the actual password for the .zip file.

Overwriting Zip Files in Linux

When extracting a .zip file that already exists in the same location, you will be prompted to choose whether to overwrite the file, skip extraction, or rename the file. For example:

The options would be as shown below:

[y]es, [n]o, [A]ll, [N]one, [r]ename

To override these options and overwrite all files without prompting, you can use the -o option.

$ unzip -o ZipFile.zip

Use caution when using this option, as it will completely overwrite existing copies of the files without prompting for confirmation.

Avoiding Overwriting Files with Unzip in Linux

If you want to extract files without overwriting any existing files, you can use the -n option. This will only extract files that do not already exist in the destination directory. For example:

$ unzip -n ZipFile.zip

Listing Contents of a Zip in Linux

To list the contents of a .zip file in Linux, you can use the -l option. This will display a detailed list of all files in the archive, including their timestamps and other details. For example:

$ unzip -l ZipFile.zip

Conclusion

That’s it! You now know the essential functions of the zip and unzip utilities in Linux. Use these commands to better manage your files.

LEAVE A COMMENT