Installing Zip and Unzip in Linux:
To begin, on Debian based Linux distributions, install both zip and unzip by running the following command:
On Red Hat based Linux distributions (such as CentOS or Fedora), you can install zip by running the command below:
On ArchLinux or Manjaro run:
Zipping or Compressing Files using Zip:
This first example shows how to zip several files into one .zip file. In the example below, zippedfile.zip is the name I give the new zipped file that contains files linuxhint1, linuxhint2, and linuxhint3. Thus, the zippedfile.zip option is an arbitrary name you can give your compressed, followed by the files you want to zip.
Based in the previous example, a new file named zippedfile.zip was created.
The second example shows how to zip similarly named files with a different end or extension. In this case, type the first part of the name shared by all files and replace the individual extension or final part of the name with a wildcard, as shown below.
The following example shows how to compress all files within a specific directory, without including the directory itself. For this, define the path and use a wildcard to refer to all files contained within the directory. In the example below, all files inside the directory linuxhint will be zipped as zippedfile4.
This example shows how to zip all files in the current directory. Just include all files by using the wildcard, as shown in the screenshot below.
Zipping directories requires the implementation of the -r flag. In this example, the directory named linuxhint will be compressed as zippedirectory.
You can instruct zip to remove original files after compression. To achieve it, you need to add the -m flag, as shown in this example.
After zipping files linuxhint1, linuxhint2 and linuxhint3, the files will be removed and remain only in their compressed format called zippedfiles5.zip.
By adding the -m option, original files will be removed after being zipped.
Unzipping or Extracting Files using Unzip:
Unzipping files is even easier than compressing them. To extract a zipped file, run the unzip command followed by the file you want to extract, as shown below.
If you type the filename without extension, unzip will detect it and extract the file.
You also can use the wildcard to extract multiple zipped files. The difference is in some cases like in the current example, you need to add a backslash before the wildcard.
In the following example, there are files named linuxhint1, linuxhint2, linuxhint3, linux-hint, and linux-hint2. I will extract only the first 3 files by applying the backslash and wildcard in the same way I did when zipping to extract all similarly named files whose last part of their name or extension is different.
To extract all files within the current directory, you also need to implement the backslash before the wildcard, as shown in the following screenshot.
In this example, there is a file called allzipped.zip which contains files named linuxhint1, linuxhint2, linuxhint3, linux-hint, and linux-hint2. The example shows how to extract files selectively, omitting specific files.
The command below instructs unzip to extract all files except linux-hint and linux-hint2.
Contrary to the previous example, if you only want to extract a few files from a zipped file, you can specify them after the zipped file, and the rest of the files won’t be extracted.
The example below instructs unzip to extract only files linuxhint1 and linuxhint2 from allzipped.zip.
You also can specify a directory for files to be extracted using the -d flag, followed by the path. In the following example the -d flag is used to store unzipped files linuxhint, linuxhint1, linuxhint2, and linuxhint3 within the linuxhint directory located in the home directory.
When extracting files to a location where the same files are stored, unzip will request manual overwriting confirmation for each repeated file.
To unzip files overwriting existing files you need to implement the -o flag, as shown in the following screenshot.
Protecting Zipped Files with a Password:
A nice zip function is the ability to protect files with a password. To achieve this, you need to implement the -e option followed by the zip file name you want to create and the file you want to zip. A prompt will ask you to type and verify the password, as shown in the example below.
To unzip a protected zip file just run unzip as normal, and type the password when requested, as shown in the following image.
Other Zip and Unzip Operations:
One of the .zip format advantages over other compression methods is that zip allows you to see the content of zipped files without the need for extraction. This is a very interesting feature that is easy to practice by implementing the -l flag.
The example below shows the use of the -l flag to show the content of the file named allzipped.zip.
You can get additional information without extracting by replacing the -l flag with -Z. This option shown in the example below will print information on file permissions, size, and creation time.
Another way to show the content of a zipped file is the zipinfo command. The following screenshot shows zipinfo displaying the content of the linuxhint1.zip file.
Zip also allows you to add files to an already zipped file. To achieve this, you need to add the -u flag followed by the zipped file and the file you want to add to the zipped file, as shown in the example below.
In the screenshot below the command zip and the -u flag are used to add file linuxhint2 to the zipped file linuxhint1.zip.
Contrary to the previous example, you also can remove specific content from a zipped file.
To achieve this, you need to implement the -d flag followed by the zipped file name and the file you want to delete from it (the -d flag is used for this purpose with the zip command and to define directories with the unzip command).
The example below shows how to remove the file linuxhint2 from the file linuxhint1.zip using the zip command with the -d flag.
As you can see zip and unzip are complete and very easy-to-use tools to deal with file compression. The zip format is a multiplatform extension and learning how to use it is advantageous for any Linux user. As said previously, it also has functions that are not present in other compression methods, such as showing contained files and directories without extraction, omitting specific files when extracting, or adding individual files to an already zipped file.
I hope this tutorial about zip and unzip files on Linux was useful. Keep following Linux Hint for more Linux tips and tutorials.