The Ultimate Guide to the Linux Zip Command

In one of our several articles, we discussed the usage of the tar command to compress a file
How to use the Tar command in Linux. This blog explains to you the use cases of the zip command on Linux

Zip: Zip is a command line utility which allows you to compress larger files and directories into a single zip file which helps to reduce disk space, makes it easier for transfer and also easy to keep as a backup.

How to create a zip file in Linux?

Create a zip file for the folder linuxlearninghub file using the command zip -r which reads the files inside the directory recursively.

zip -r linuxlearninghubfiles.zip linuxlearninghubfiles

In order to unzip the zip file above created use the unzip command

unzip linuxlearninghubfiles.zip

While unzipping this will create a directory called linuxlearninghubfiles and unzip the contents in it so if we are unzipping the .zip file within the path which contains the same folder which we have compressed it will show a message whether replace the existing file’s as shown below

replace linuxlearninghubfiles/test.txt? [y]es, [n]o, [A]ll, [N]one, [r]name

How to add a file to an existing zip file?

zip -u linuxlearninghubfiles.zip newfile.txt

Using the -u switch we can add a new file to the existing zip file. In the above command, we are adding newfile.txt file to the .zip file.

How to remove a file from an existing zip file?

zip -d linuxlearninghubfiles.zip removefile.txt

We can remove a file from the existing zip file using the -d switch. Here we are removing the removefile.txt file from the existing zip file.

Extract Zip File to a Specific or Different Directory

unzip linuxlearninghubfiles.zip -d /home/testfolder

We can use the -d switch along with the path of the folder in order to extract a zip files to a specific directory in Linux.

List the contents of a zip file

To list the contents of a zip archive, use the zipinfo command followed by the name of the zip file.
For example, to list the contents of the myzip.zip archive, you would run

zipinfo myzip.zip

Summary:
These are just a few examples of how you can use the zip command in Linux. There are many other options and features available, so be sure to check the zip and unzip man pages for more information.

Leave a Reply

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