Use of Zip and Unzip command in Linux

What is Zip ?

Zip is a file format that helps to compress one or more files together into a single location. As an archiving tool, zip is used for backup files as well as for transferring the files between servers. The zip file format supports the lossless compression algorithm. In this article, we are discussing about the use of zip and unzip command in Linux.

Zip Features 

Zip is not only for archiving the files, it will provide more features and are following

  • Different Compression levels
  • Split archives
  • Encryption

Compression levels help us to add compression to the zip archive to reduce the size of the archived file. The compressed files help to keep the backups with less size. If we have a large folder zip uploaded to a backup space with less upload size limit, then we can use the archive split option to split the archive and upload it into the backup space. If we need to transfer a large directory between servers, it takes a large time based on the number of files and the size of the directory. But if we compress the directory with high-level compression it will help to transfer the data between the servers with speed.

The encryption option in the zip archive will help to transfer private data in a secure way. It will avoid accessing the data by a third party.

Zip and Unzip command Binary 

Binary files for zip and unzip command.

/usr/bin/zip
/usr/bin/unzip

Following are the rpm ( Red-hat Package Manager ) used for zip and unzip commands.

$ rpm -qf /usr/bin/unzip /usr/bin/zip 
unzip-6.0-5.el6.x86_64
zip-3.0-1.el6_7.1.x86_64

Usage of zip and unzip command in Linux. 

Adding files to the zip archive 

For adding one or more files to a zip archive by providing the files as arguments with space separation. Following are some sample examples.

Add one file to zip

zip archive.zip file1.txt

Add multiple files to zip

zip archive.zip file1.txt file2.txt file3.txt file4.txt 

Add multiple files to zip archive

 

Deleting the files from the zip archive 

In some cases, we need to delete one file from the archive and in a normal case, it takes multiple steps like, unarchive the zip delete the file, and zip again. We can use the option “-d” to delete the file without unarchiving the entire zip file. Following is the command for doing the same.

zip -d archive.zip file1.txt 

Deleting the files from the zip archive 

Update one file in the zip archive 

In some cases, the version update of a software package includes only a few file changes and it can be accomplished using the file update option in the archive. Following is the command to do the same.

zip -u archive.zip file1.txt 

Update one file in the zip archive 

Delete the original files after zipping

In some cases, the user will not delete the files after archiving and it can be avoided with this option. This can be achieved using the option “-m”. The deletion of the files will occur only after the zip file is created without any errors. Even though this is useful for conserving the space, it is dangerous to delete the input files. Following is the command for the same.

zip -m archive.zip file1.txt file2.txt file3.txt file4.txt 

Delete the original files after zipping

Archive a directory using zip 

If we need to archive one directory, then the ‘-r’ option will help to achieve this.

zip -r archive.zip directory/

Archive a directory using zip 

Exclude files from zip archiving 

Some files can be excluded from zipping the archive using the exclude option. If we are zipping every file in a directory excluding some files only, then we can use the “-x” option to use this. This option can be used along with the other zip options, mentioned as an additional switch. Following are the commands for excluding one file and with similar patterns.

zip archive.zip * -x test.txt
zip archive.zip * -x test.*

Compression in zip 

There are 9 compression levels in the zip archiving, “0” means no compression and “9” is the optimal compression. The default compression level is “6”. Following is the command used for adding compression level to the above directory zipping.

zip -9 -r archive.zip directory

Encryption in zip 

The sensitive information can be stored in a zip archive using password protection and it can be achieved using the “-e” option. It will ask for the password twice ( for verification ) and after that, it will be password protected. And for unarchive the zip file using the normal method and it will ask for the password while unarchiving.

zip -e -r archive.zip directory 

Encryption in zip; zip and unzip command in linux

Splitting the zip archive 

If we need to upload a 5G zip backup to a backup location and the backup provider has an upload file size limit of 1G, we can use this split option to split the archive and upload it without any hassle.

zip -s 1g -r archive.zip directory

Verbose option in zip. 

Every command has a verbose option to know more details about the activity running behind it. Like that zip also have the verbose option with the switch “-v”. It will print more additional data in the archiving process which can be used for detailed analysis of the data compression. Following is the command used for the same.

zip -v archive.zip *

List of files in zip archive

If a file list is specified as “-@”, zip takes the list of files from the standard input instead of the command line. The option is mainly used along with the find command. The sample command along with the find is referred to here. We can replace the find command with your own commands depending on the situation.

find . -name "*.txt" -print | zip source -@

Unzip the zip archive 

Unzipping an archive is a simple command like in zip

unzip archive.zip

List contents of zip archive without unarchiving

Sometimes we need to verify the contents of the zip file before unarchiving and it can be accomplished using the “-l” option. Following is the command for the same.

unzip -l archive.zip

How to install zip and unzip command. 

There are times when the server doesn’t have a zip command; in that case, it’s necessary to install the commands. The following command can do that.

Redhat based systems 

yum install zip unzip

Debian based systems 

apt install zip unzip

Zip files are safe? 

Zip files are safe. Although, users should be careful when extracting zip files downloaded from the internet, as some contain viruses, zip bombs, trojans, and other types of malware.

Conclusion 

We discussed how compression and decompression of a zip archive work and referred to the use of zip and unzip commands from the linux command line. Tar files are better for Linux, since they are more flexible and preserve file permissions, but in some situations, we need to use zip files as well.