In this tutorial, we touch base on the zip and unzip commands and how they are used in Linux. Zip is a command used for creating an archive file or a zipped file. This allows you to compress your files and create more space for other files on your hard drive or removable drive. Additionally, zipping your files/directories makes them more portable and easier to upload, download or even attach and send them via email.The opposite of zipping is unzipping, and here we use the unzip command to decompress the files and access them individually.
Before start discussing zip and unzip command, let’s see how these commands can be installed from command line,
Install zip & unzip command on Ubuntu / Debian / Linux Mint
Open the terminal and run the following apt command,
$ sudo apt install -y zip unzip or $ sudo apt-get install -y zip unzip
Install zip & unzip command on CentOS / RHEL / Fedora
Open the terminal and execute the beneath command,
$ sudo yum install -y zip unzip or $ sudo dnf install -y zip unzip
Let’s dive in and see how to zip and unzip files and directories in Linux with zip and unzip command with examples.
Zip command in Linux
Zipping a file is as easy as ABC. The syntax is quite straightforward:
$ zip OPTIONS archive.zip file
A few points to take into consideration before you proceed:
- You need to have write permissions on the file you are zipping and the directory location before zipping a file.
- When you unzip or extract a file, the user owns the file/directory.
Example 1) Zipping a single file and multiple files with zip command
Let’s assume you have a text file – reports1.txt – in your current directory and you want to zip it into an archive called reports.zip.
The command for this operation will be:
$ zip reports.zip reports1.txt
Additionally, you can zip multiple files at a go into an archive as shown:
$ zip archive.zip file1 file2 file3
Again, let’s assume that we have 3 text files reports1.txt, reports2.txt and reports3.txt. To zip them into the archive reports.zip, run the command:
$ zip reports.zip reports1.txt reports2.txt reports3.txt
Example 2) Adding a file to a zip archive (-u)
At times, you may find the need to add a file to a zip archive. To do so, use the -u flag. For example, to add another file reports4.txt, run:
$ zip -u reports.zip report4.txt
Example 3) View contents of a zipped file
To view contents of a zipped file, use the command as shown:
$ zipinfo archive.zip
Example 4) Zipping all the files in the current directory (* )
If you have multiple files in your current directory, you can zip all of them at a go using the wildcard symbol as shown in the syntax below:
$ zip archive.zip *
For example, to compress all files in the home directory to home.zip archive, execute the command below. Be sure that you are working in the home directory.
$ zip home.zip *
Example 5) Delete a file from an archive (-d)
To remove a file from an archive, invoke the -d flag. For instance, to remove reports4.txt from the zipped …