How to Zip and Unzip Files in Linux
File compression is a powerful mechanism to save storage space and facilitate easy file transfers, especially when the file size is huge. There are various formats of compressed files such as .zip, .rar, etc. But, zip remains the most commonly used format. If you’re on a Linux system, zipping and unzipping files (compressing and decompressing) is a task that you can accomplish from the command line interface as well as the GUI. So, in this article, we have discussed how to zip and unzip files in Linux using these two methods.
For those unaware, zipping means compressing a file or multiple files of different formats into a single file, such that it occupies less space and can be transferred using less bandwidth. Unzipping a file means extracting all the compressed files from a zip file. That said, let’s look at how you can zip and unzip files in Linux:
How to Zip Files in Linux (GUI Method)
The GUI method to zip and unzip files is easier compared to the CLI method but lacks various options and features which may be useful for many users. The method shown here will work for allLinux distributionswith any file manager. For this tutorial, we are using the Nautilus file manager in Ubuntu 20.04 LTS version.
-
First, open any file manager of your choice and navigate to the directory where your files are located.
-
Then, select the files you want to add to the zip file.
-
Right-click on any one of the selected files and select“Compress.”
-
Here, you need to give your compressed file a new name and select the file format – .zip, .7z or .tar.xz. Finally, click on“Create”at the top right.
-
And that’s it. The compressed file will be saved to the same directory with the selected format.
How to Unzip Files in Linux (GUI Method)
There are three different ways to unzip an archive file using the GUI File manager on Linux:
1. Extract All Files in the Same Directory
2. Unzip All Files in a Different Directory
3. Unzip Only Selected Files
How to Zip Files Using Linux Command Line
The CLI method of compressing and decompressing files is not only faster compared to the GUI method, but it also provides a lot of options to tinker with files. While there are multiple commands to compress and decompress files,zipandunzipare the most common commands as they are cross-platform and have wide online support.
1. Install Zip and Unzip Packages
By default, both the packages – zip ad unzip – come pre-installed on most distros. Use the commands below to install them, if it is not available by default:
sudo apt install zip
sudo apt install unzip
2. Compress Files Using Zip Command in Linux
The basic syntax to compress files using thezipcommand is as follows:
zip file.zip file(s), where
Some of the common options to pair with the zip command are:OptionsDescription-dUsed to remove a file from the zip archive.-uUsed to add new files to a zip file.-mThis option will delete the original files after zipping them.-rIt is used to zip an entire directory.-xThis option is used to exclude the files while zipping.
3. Zip a Single File in Linux
To compress a single file, use the following command:
zip file.zip filename
This will compress the specified file in the current directory while keeping the original file intact.
4. Zip Multiple Files in Linux
For compressing multiple files into a single zip file, use the command given below:
zip zip_file file1 file2 file3
This will compress all the specified files into a single zip file.
5. Add Files to an Existing Zip File
Sometimes you might miss a file while compressing multiple files. You can add a single file to an existing zip file. This can be done using the-uflag, which stands for the update. To add a new file to an existing zip file, use the following command:
zip -u zip_file new_file
This command will add thenew_fileto the existingzip_file
6. Remove File from an Existing Zip File
While zipping multiple files, it may be possible that you may have added a sensitive file to the zip file. Thankfully, you can easily delete a file from an existing zip file by using the following command:
zip -d zip_file file_to_be_deleted
Here, the -d flag stands for delete which will delete the filefile_to_be_deletedfrom the existingzip_file.
7. How to Zip a Directory in Linux
In some cases, you might want to zip the entire contents of a directory, but writing all the file names one-by-one in the command is not feasible. In such a case, you can use the-rflag to recursively or repeatedly add the files of a directory to the zip file.
zip -r zip_file directory_name
This will add all the files indirectory_nameto thezip_file
8. Create a password-protected zip file
Some sensitive files need to be password protected to prevent unintended users from accessing them. One way to protect the files is to convert the files to zip and encrypt them using -e (encrypt flag). To create password protected zip file inLinux, use the following command:
zip -e zip_file file_1 file_2
You will be asked to enter and verify a password for the compressed file.
Also, you can also add multiple flags while compressing using the below command:
zip -r -e zip_file directory_name
Here, we are encrypting all the directory contents into a zip file.
How to Unzip Files Using the Linux Command Line
Decompressing a zip file means extracting all the information stored inside a zip file using various commands. In Linux, the most common command to decompress a zip file is theunzipcommand.
The basic syntax to use the command is:
unzip zip_file, where
1. View Contents of a Zip File
It is always advisable to view the contents of a zip file before extracting it, so as to check for any malware waiting to harm your system. In Linux, you can view the contents of a zip file without extracting it, using the following command:
unzip -l zip_file
To view more information about the Zip File, use the-Zflag, as shown below:
unzip -Z zip_file
2. Decompress a Single Zip File
To decompress a single zip file in the same directory, use the following command:
unzip zip_file
3. Unzip a Single File to a Different Directory
If you want to unzip a zip file to a different directory, use the-dflag as shown below:
unzip zip_file -d path/to/directory
4. Unzip Multiple Files in Linux
Suppose there are multiple zip files and you want to extract them all. Use the modified version of unzip command to extract all the zip files as shown below:
unzip “*.zip“
Understanding the command: Here, we are looking for all the files with the extension.zipwhich is specified by the wildcard”*” which is then passed to the unzip command one by one.
5. Decompress a Zip File Excluding Some Files
If, after viewing the contents of a zip file, you want to extract only some of the files. It can easily be done using the unzip command in Linux. To extract a zip file while excluding some files, use the -x flag to specify the filename you want to exclude as shown in the following command:
unzip zip_file –x file_name
We all need to zip and unzip files irrespective of the operating system we are working on. Zip files can sometimes help to save a lot of precious memory by compressing the files. In this article, we have discussed two methods to zip and unzip files in Linux with examples. Using the command line method is faster, provides more options to work with, and is the preferred method for most Linux users. Do let us know in the comments if you face any issues with the zip and unzip commands
The zip command is a popular command-line tool used to compress files in Linux. It has many options such as combining multiple files and directories into a single zip file, compressing files with a password, etc. The unzip command is used to decompress or extract the contents from a zip file.
Zip is an archive file format that is used to compress one or more files together into a single zip file. It reduces file size without losing data which makes it easier to transport or store. The unzip tool is used to extract a zip file in the original format.
A zip file is an archive format containing multiple files combined and compressed into a single file. Being in compressed file format, a zip file occupies less space and consumes less bandwidth while transferring.
Beebom Staff
Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.
Add new comment
Name
Email ID
Δ
01
02
03
04
05