How to Zip and Unzip Files in Linux Terminal

How to Zip and Unzip Files in Linux Terminal. In this post, we introduce Zip file, its advantages then show you how to Zip and Unzip files in Linux terminal.

What Is a Zip File

Zip is a free and open source file and directory compression tool for Linux, MacOS, and Windows operating systems. A common file format that reduces the file and directory size and makes it easier to transfer files to other computers. The full form of Zip is “Zip Archive File” which supports lossless data compression. So you don’t need to worry to lose any information during compression or decompression. The Zip file format was created by Phil Katz and Gary Conway in 1989. It has the ability to add multiple files into a single compressed file. A Zip file is considered a base file format by many programs and software.

Unzip is a file decompression utility used to extract the zipped file. In simple words, Unzipping is the process of extracting files from a zipped archive.

Advantages of Zip File

  • One of the major advantages of a Zip file is that it saves storage space and increases the efficiency of your computer.
  • Provides an effective way to transfer files via email and chat.
  • The Zip file also maintains your privacy by encrypting your file content.

We have arrived to the main part of the article. Please follow and learn How to Zip and Unzip Files in Linux Terminal.

How to Zip Files in Linux Terminal

This section explains how to Zip a file and directory in Linux.

Prerequisites

  • A system running the Linux operating system.
  • A root user or a user with sudo privileges.

How to Install Zip and Unzip

By default, the Zip and Unzip packages are available in the default repository of all major Linux operating systems. Install it using any package manager.

For Ubuntu and Debian based operating systems install Zip and Unzip utilities with the following command.

				
					apt install zip unzip -y
				
			

For CentOS, RHEL and Fedora based operating systems, install Zip and Unzip utilities with the following command.

				
					dnf install zip unzip -y
				
			

After installing both utilities, verify the Zip package version with the following command.

				
					zip --version
				
			

You should see the Zip package information on the following screen.

Basic Syntax of Zip Command

The basic syntax of the Zip command is shown below.

				
					zip [options] [outputfile] [file1] [file2] ... [filen]
				
			

A brief explanation of each supported option is shown below:

  • r – Recursively compress all files in a specified directory.
  • -d – Remove the file from the Zip archive.
  • -u – Add a new file to the existing Zip archive.
  • -m – Removes original files after zipping.
  • -x – Excludes a specific file when you are creating a Zip file.
  • -v – Prints zipping progress status.
  • -e – Create a password protected Zip file.

Compress a File and Directory Using Zip

The Zip utility allows you to compress one or more files into a single archive.

For example, to create a Zip archive of a single file named cloud1, run the following command.

				
					zip -v cloud1.zip cloud1
				
			

You should see the following screen.

After creating a Zip archive, you can use the -T option to check the integrity of the Zip file.

				
					zip -T cloud1.zip
				
			

You should see the following screen.

You can use the -m option to delete the original file after compressing it to a Zip archive.

				
					zip -vm cloud1.zip cloud1
				
			

You should see the following screen.

To create a Zip archive of multiple files named cloud1, cloud2, cloud3, and cloud4, run the following command.

				
					zip -v cloud-multi.zip cloud1 cloud2 cloud3 cloud4
				
			

You should see the Zipping process on the following screen.

The Zip command uses the -r option if you want to create a Zip archive of the directory.

				
					zip -vr clouddir.zip clouddir
				
			

This command recursively searches all files inside the clouddir directory and subdirectories then compress them as shown below.

Remove a File from a Zip File

If there are multiple files inside the Zip archive and you want to delete some unwanted files from the Zip archive then you use the -d option.

The following command removes files named cloud1 and cloud2 from the cloud-multi.zip file.

				
					zip -d cloud-multi.zip cloud1 cloud2
				
			

You should see the deleted files on the following screen.

Add a File to a Zip Archive

Also add some additional files to an existing Zip archive using the -u option.

For example, to add some additional files named cloud5 and cloud6 to the cloud-multi.zip file, run the following command.

				
					zip -vu cloud-multi.zip cloud5 cloud6
				
			

You should see the added files on the following screen.

Create a Password Protected Zip File

What is more, Zip also allows you to create a password protected Zip archive. This helps you to protect your important files. Use the -e option with the Zip command to create a password protected file.

The following command creates a password protect Zip archive of the file named protected1.

				
					zip -ev protected1.zip protected1
				
			

You will be asked to provide a password as shown below.

Exclude Files from the Zip Archive

Please use the -x option if you don’t want to include a specific file in a Zip archive. The following command excludes a file named cloud1 from the clouddir directory.

				
					zip -xrv clouddir.zip cloud1
				
			

Create a Split Zip File

You may need to split the Zip file if your created Zip file is very large and unable to send it via email due to the size limit. The zipsplit comes with the Zip package that allows you to easily split a Zip file into a set of smaller Zip files.

To split a Zip file of 24MB size into a 4MB file, run the following command.

				
					zipsplit -n 4096000 bigfile.zip
				
			

This command splits the bigfile.zip into six files with a size 4MB of each.

How to Unzip Files in Linux Terminal

In this section, we show you how to Unzip a compressed Zip file in Linux.

Basic Syntax of Unzip Command

The basic syntax of the Unzip command is shown below.

				
					unzip [options] [zip-file]
				
			

A brief explanation of each supported option is shown below:

  • -V – Display verbose output.
  • -n – Prevent overwriting existing files.
  • -x – Exclude specific files from the Zip archive.
  • -o – Overwrite existing files without any prompt.
  • -d – Extract a Zip file inside a specified directory.
  • -l – List the content of the Zip archive.

Unzip a Compressed File

You can use the Unzip command to extract the content of the Zip archive.

The following command Unzips the file named cloud-multi.zip to your existing directory.

				
					unzip cloud-multi.zip
				
			

You should see the unzipping progress on the following screen.

Unzip a File to a Specific Directory

In some cases, you may need to extract the Zip file in a specific directory. In this case, use the -d option to define your target directory.

The following command extracts the cloud-multi.zip file to the /tmp directory.

				
					unzip cloud-multi.zip -d /tmp
				
			

You should see the following screen.

List the Content of the Zip File

Additionally, also see the list of files available inside the Zip archive file using the -l option. Run the following command to see all files:

				
					unzip -l cloud-multi.zip
				
			

You should see all files on the following screen.

Unzip a Password Protected Zip File

To Unzip a password protected Zip file, you need to provide a password to extract the Zip archive. Run the following command to Unzip a password protected Zip file.

				
					unzip protected1.zip
				
			

This prompts you to provide a password to extract the Zip file as shown below.

You can also use the -P option to provide a password with Unzip command.

				
					unzip -P yourpassword protected1.zip
				
			

Now, your protected Zip file will be extracted without asking you a password.

Thank you for reading How to Zip and Unzip Files in Linux Terminal. We shall conclude this article now. 

How to Zip and Unzip Files in Linux Terminal Conclusion

In this post, we have explained how to Zip and Unzip a file in a Linux terminal. We also used different options with Zip and Unzip commands and showed you real life examples. I hope this guide assists you to Zip and Unzip a file in your day to day tasks. Overall, the Zip utility is a handy tool to reduce the file and directory size and transfer them easily to your friends and other systems.

Do explore more of our Ubuntu content by navigating to this section of our blog. 

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x