Zip is one of the most popular cross-platform archive file formats. Zip can be used for multiple reasons. They are used to transfer multiple files as a single unit by archiving them to a single file. It can also be used to compress files. Which will reduce file size and thus help in the transfer of large files and also help by reducing the storage space.
The extension of the Zip file format is .zip. The compression ratio of Zip files is in the ratios of 2:1 to 3:1 for text files. These ratios change when we are archiving files other than text files like MP4, Mp3, etc. Zip has a compression method where files can be archived without compressing. It is very useful in the distribution of files. For example, If you want to send your brother some family photos of a function rather than sending photos by photos. You can zip the photo folder and send it to him. Once he unzips the zipped file, he has all the family photos.
In this article, let me tell you the zip commands in Linux.
Install Zip and Unzip
First, let us install zip
Zip usually comes pre-installed in Linux. If you don’t have or it is missing in your system, then install them using the commands below. And use all the zip commands in linux.
Launch terminal using Ctrl+Alt+T
To install Zip:
Without zip installed in the system, we cant use zip commands in linux.
$ sudo apt install zip
To Install Unzip:
$ sudo apt install unzip
$ zip [options] filename
$ unzip filename.zip
Options are certain commands that tell zip to do a certain action. For example, delete a file from the zip file or extract a particular file only, etc. There are many options in zipping, they are:
- -d Option
- -u Option
- -m Option
- -r Option
- -v Option
- -x Option
- -e Option
The purpose of using the – d option is to remove a file from the Zip archive. Once after zipping a group of files, now you wanted to remove a particular from the zip archive, Then you can use the – d option to do it. For example, there is a folder named Linuxstro. In the Linuxstro folder, there are 5 text files. Like,
Now you have zipped the Linuxstro folder. And after a while, you wanted to remove a2.txt from Linuxstro.zip. In this situation, you can use the – d option,
$ zip -d Linuxstro.zip a2.txt
Now the a2.txt is deleted from the Linuxstro zip folder. Once you extract Linuxstro.zip, you will get all four files except a2.txt.
It is used to update a file or list of files to an already zipped archive. For example, take the previous case. After zipping Linuxstro.zip, now you wanted to add another file named a6.txt. In this case, we can use the – u option.
$ zip -u Linuxstro.zip a6.txt
Using the – m option deletes the original files after the successful zipping without any errors. For example, After creating a zip of all .txt files from the Linuxstro folder successfully. All the .txt files in the folder would be deleted. This helps in disk storage.
Let the name of the zip be Linuxstro.
$ zip -m Linuxstro.zip *.txt
This option is used to zip a directory. For example, in the current directory, there is a folder named Folder and now you want to Zip this folder. In this case, we can use the -r option.
Lets us name the zip file ZippedFolder,
$ zip -r ZippedFolder.zip Folder
V stands for verbose. Using the -v option will display the progress and many other details about the target environment, compiler type, os version, date, etc.
$ zip -v file-name.zip file.txt
When we are zipping all the files in a folder, and if you wanted to exclude some files from the folder getting zipped. We can use the -x option to attain this. For example, In a folder, there are many files, and you wanted to exclude a file named 1a.txt.
Let the zip name be Linuxstro and a1.txt is the file to be excluded.
$ zip -x Linuxstro.zip 1a.txt
7.) -e option
The -e option is used to create zip files with password protected.
Let the name of the zip be Linuxstro and and the file name be 1a.pdf.
$ zip -e Linuxstro.zip 1a.pdf
Then the system will ask you to Enter the password. Once entered You have created a password protected zip file.
Commands in Zip and Unzip
So far we have gone through the option that zip has. Now let us dive into the commands that are used in day-to-day life.
Single file Zip command
$ zip zipfilename.zip file
Multiple files Zip command
$ zip zipfilename.zip 1a.txt 2a.txt 3a.txt
Zip a particular format files
Suppose there are multiple files with different files formats. If you wanted to zip all of a particular files only , the we can do it. For example, A Folder contain different files such as .txt files, .mp3 files,.mp4 files, etc. Now you wanted to zip all .txt files.
Lest see how to do it.
$ zip zipfilename *.txt
Zip a file and store it in a different Folder
Suppose you need to zip file1.pdf as pfile.zip and wanted to store it in a different directory, Then you can do it.
$ zip path_to_Folder/pfile.zip file1.pdf
Add File to an existing zip
If you have already made a zip and missed any files to add, then after zipping you can add another file to the existing zip. For example, There is an existing zip named Group.zip and you wanted to add a file named student.pdf.
$ zip -u Group.zip student.pdf
Delete a File from the Zip
Suppose you have already made a zip and you wanted to delete a file from that zip. For example, the zip file is Group.zip and you wanted to delete studient.pdf, then
$ zip -d Group.zip student.pdf
Quit mode – Does not display the output
In this it just performs the operation and doesn’t display the output.
$ zip -q Group.zip student.pdf
// student.pdf is zipped to Group.zip
Delete Files after Zipping
After making the zip files, once the zipping is successful then with the help of this command you can delete the original files.
$ zip -m Group.zip student.pdf
// student.pdf is zipped to Group.zip
//student.pdf is deleted
Zip a Directory
We can recursively zip a folder.
$ zip -r Group.zip Groups/
// Groups is the directory
Set Level of compression
Zip has a compression level from 0 to 9. Compression value is set to 6 by default. 9 is the highest value of compression and 0 is the lowest.
// Higest level of compression
$ zip -9 Group.zip student.pdf
// Lowest level of compression
$ zip -0 Group.zip student.pdf
Creating a password protected Zip file
You can create a password protected zip. So to extract that you will need to enter the correct password.
$ zip -e Group.zip student.pdf
==> Then the system will ask you to Enter password. Once entered You created a password protected zip file
Add password to an Existing Zip file
Suppose you have created a zip file and you forgot to add password for the zip file. Then using the command below you can add password to existing zip file.
$ zipcloak Group.zip
==> Then the system will ask you to Enter password. Once entered You have sucessfully added a password to an existing zip file
View details of the Zip file
$ zipdetails Group.zip
This command will show the details of the zip file.
Also you can use zipinfo command to display information about the zip. Both of these commands returns different details of the zip file. Using zipinfo you can get informations like permission,size,date etc.
$ zipinfo group.zip
Search text Inside the zip
With the help of zipgrep command. We can search a tetx inside the zip file without extracting the zip file. If the zip file is password protected, then you should first enter the password for it.
For example, the let zip be Group.zip and and text to search is ” Castro”.
$ zipgrep Castro Group.zip
To extract zip just use the unzip command.
$ unzip Group.zip
Unzip File to a Particular Folder
Incase if you wanted to extract the files to different folder.
$ unzip Group.zip -d /path-to-the/directory
Unzip Multiple Files
Supose in the directory there are many zip files. Rather than extarcing one by one, we can extract all files with a single command.
$ unzip '*.zip'
Quit mode -Doesnt display output.
In this mode it just perform the operation. It wont display any details about the operation.
$ unzip -q Group.zip
While extracting a zip, there would be multiple files. Supose you don’t want a particular files to be extracted then you can do it with the following command.
Let the zip be Group.zip and file not to extract be student.pdf
$ unzip Group.zip -x student.pdf
Unzipping password protected zip files.
We can use the option -P. And then the password.
$ unzip -P password Group.zip
Usually when you are etxrating a zip and if the files already exist. We will get prompt to overwrite,skip,rename,etc. But if you want to overwrite without getting prompt you can use command below.
$ unzip -o Group.zip
$ unzip -l Group.zip
These are the zip and unzip commands that are being used mostly. We have learned many Zip commands in Linux. Make sure you follow the syntax. As for the purpose of examples we used keywords such as Linuxstro.zip, Group.zip ,student.pdf and many .txt. They are examples for those particular file names and are not commands. We have covered most of Zip commands in Linux.