Zip is an archive file format that supports lossless data compression. Zipping and unzipping tools are available for various platforms. Windows and macOS and many other OS are providing built-in zip support. These files generally use
.ZIP file extension. A zip tool allows you to specify the compression level from 1 to 9. Ahead in this article, we will discuss creating a zip file in Linux.
How to install the zip tool?
As discussed above many operating systems provides built-in support for the zip file format. But it is possible that some Linux distributions do not have a pre-installed zip tool or command. If it is not installed in your system then install it by using the following command –
Debian Based Ubuntu, Linux Mint, etc:
If you are using a Debian based distribution such as Ubuntu, Linux Mint, etc. then use –
sudo apt-get install zip
RPM-based CentOS, RedHat Linux, etc:
If you are using a system that uses yum package manager then use –
sudo yum install zip
To install it in fedora use –
sudo dnf install zip
How to create a Zip archive file in Linux?
The syntax of zip command is given below –
zip [options] file.zip file(s)
Creating a zip archive from a directory –
To create a zip archive of a directory named sample which contains some file and directories inside it, use the following command in your terminal –
zip -r sample.zip sample
Now a file named sample.zip will be created inside the current working directory.
Creating zip archive from files
Suppose you have different files in your current working directory then you can create a zip archive of these files by using the following command –
zip myzipfile.zip file(s)
For example –
zip myzipfile hello.txt hello1.txt hello2.txt hello3.txt
This will create an archive named myzipfile.zip in the current working directory.
Creating a zip archive of all text files in the current working directory –
Use the following command –
zip myzipfile *.txt
Deleting original files or directories after creating a zip archive –
If you want to delete the original file after creating a zip archive then you need to use
-m option with zip command –
For example –
zip -r -m sample.zip sample
Here target directory named sample will be deleted automatically after sample.zip is created.
There are many options available that you can use with the zip command. Run the zip command without any arguments to display the options –