How to zip and unzip files from Terminal in Linux

  1282 views   9 months ago Ubuntu

Zip is the most common feature to compress the file and transfer the files easily. With Zip, we can also send the directory directly. Also when we work in command-line server, there is no option we can zip file from the GUI. Also it is fast to zip or unzip files from command-line.

In this article, we will discuss on how to zip and unzip the files in Linux OS systems from the command line.

Zipping the files

Zip comes preinstalled in the most Linux desktop environment. You can check it from the bellow command.

zip --version

You will get bellow response if zip is already installed. 

Or you can also install zip with just bellow command

Ubuntu and Debian based OS distribution

sudo apt-get install zip

CentOS and Fedora based OS distribution

sudo yum install zip

To zip the file, just use the bellow command.

zip filename.zip file1 file2 file3

This will create filename.zip file.

Unzip the Zipped file

First install the unzip from the bellow command.

Ubuntu and Debian based OS distribution

sudo apt-get install unzip

CentOS and Fedora based OS distribution

sudo yum install unzip

The bellow command is the basic sintax to unzip the zipped file. It will unzip all the files in the currenct folder without folders.

unzip filename.zip

Use the -d option if you want to unzip the files in the specifies directory.

unzip filename.zip -d /path/to/directory

If you want to see the content in the zipped file without unzipping, the use -l option.

unzip -l filename.zip

This will display content of the zipped file.

If your zip file is password protected, then use the -p option with password.

unzip -p filepassword filename.zip

If you do not want to unzip all the files and want to exclude some file, the use -x option with exclued files.

unzip filename.zip -x file1 file2

If you want to overwrite the file without asking, use -o option.

unzip -o filename.zip

Or if you want don't want to overwrite the file, then use -n option with file name

Unzip the Rar file

If you want to unzip the rar files, then first, you need to install unrar.

Ubuntu and Debian based OS distribution

sudo apt-get install unar

CentOS and Fedora based OS distribution

sudo yum install unrar

To unzip the rar file to current directory

unar backup.rar

Check out all the options available for Unrar.

unar man

Conclusion

This way unzip will help you to work with files. If you have any question or suggestion, please feel free to comment bellow.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]