How ToLinux

How to Zip and Unzip files in Linux [Compress & Extract]

To Zip and Unzip files on Linux, You have to use Command Line Interface. There is no tool available to zip or unzip on linux.

Combining a lot of files and folders into a single shareable file is one of the best ways to share multiple files to a person. In Windows, you have apps like WinRAR, Winzip to do that. But in Linux PC, you need to do those things using CLI (Command Line Interface). You cant install any apps to do those functions. In Linux, you can zip and unzip a file by using a single line of code. Are you in the need to zip or unzip a file in Linux and don’t know how to do that? Well, in this article you will learn how to zip and unzip a file in Linux.

How to Zip and Unzip Files in Linux?

You don’t need any tools to zip or Unzip a file in Linux. Just follow the steps below zip/Unzip a file.

What is Zip or Zipping a file?

Zip or zipping a file means compressing a folder containing one or more files into a lesser size. By zipping a folder, you can reduce the size of the folder and also protect the files in the folder. The converted file will be in the format .zip. But there are other formats of zip file also available: .tar and .gz are notable mentions.

How to Zip and Unzip files in Linux

How to zip files in Linux?

You can zip a file or files in Linux, using a single command. Below is the syntax of zipping files in Linux

zip filename filea fileb filec

For instance, if you want to compress files named one, two, three to a zip file techowns, write the zip command

Example: zip techowns one two three

This is the simple command that can be used to zip files. Like this, there are many commands that can zip files with the complex operation.

Zip all the files

zip -r home

This command will zip all the files and folders in the working directory.

Add files to a zip file

Now, if you want to add files to the already zipped file, you can do the same in Linux by using some changes in the zip command.

zip -r techowns /tech

The above command will zip all the files from the tech folder and them in the existing zip file techowns.

Delete a file from a zip file

You can also delete or remove a file from the zip folder without affecting it.

zip techowns -d technical

The above command will delete the file named technical from the zip file techowns without affecting the files present in it.

Copy files from one zip to other

You can also copy files from one zip folder to another without unzipping in it. In Windows OS, you need to unzip it and copy it.

zip techowns.zip -U –out technical.zip “internet.txt”

The above command will copy the internet.txt file from the techowns folder to add it in the technical.zip file.

Encrypt zip file

You can also protect your zip file with a password so that if you unzip the file, it will ask for the password.

zip techowns.zip -r /home/wikileaks -e

Executing the above command will ask for a password two times. After that, whenever you try to unzip the techowns.zip file, you need to enter the correct password.

Exclude a file from zipping

Like adding all the files, you can also exclude a particular file from zipping it.

zip techowns.zip -r /home -x *.mp4

The above command will zip all the files in the directory folder into a zip file named techowns without the .mp4 file format.

That’s all about the zipping files in Linux. Read the rest of the article to unzip the files

What is Unzip or Unzipping a file?

Unzip or Unzipping a file means that extracting or expanding the combined files or folder. Like zipping, unzipping also has different formats of files.

How to Zip and Unzip files in Linux

How to Unzip a file?

Unzipping a file is also a simple process like zipping but you need to aware of what is the format of the zipped file.

unzip techowns.zip

The above command will unzip the file named techowns. But it will throw an error if the file named techowns is in the .tar or .gz format

In some Linux OS, unzip is not installed by default. If it is not installed by default, install using the below commands.

sudo apt install unzip

The above code will install the unzip on your Debian. If you want to install unzip on CentOS and Fedora, type the below command.

sudo yum install unzip

Unzip .tar files

tar xvf techowns.tar

The above command will unzip the file named techowns.tar

Unzip .gz files

gunzip techowns_tar.gz

tar xvf techowns_tar

The above two commands will unzip the techowsn.gz file without any error.

Unzip a file to another directory

You can unzip a zipped file to another folder if you want. If you unzip to another directory, you can eliminate the unwanted repetition of files.

unzip techowns.zip -d /pc/computer

The above command will unzip the techowns.zip file to the new directory computer.

Unzip the password-protected zip file

If your zip file is password protected, you can use the below command to enter the password to the protected zip file.

unzip -P Password techowns.zip

Excluding files from unzipping

You can unzip the file by excluding a particular file.

unzip techowns.zip -x computer

The above command will extract all the files from the techowns.zip file expect the file named computer.

Unzip multiple zipped files

You can unzip two or more zipped files in a single command.

unzip ‘*.zip’

The above command will extract all the zipped files in the directory that is in the format .zip

UseFul Link: How to Unzip Files on Android

Our Opinion

These are the commands that you can use to zip and unzip a file in Linux PCs. Unlike Windows and macOS, Linux mostly works with command-line codes. You can easily perform all activities related to zipping and unzipping using the above commands.

Did we miss out something? Tell us in the comment section below. Also, follow us on Facebook and Twitter.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *