How To Zip And Unzip Files In Linux Guide
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 all Linux distributions with 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
First, open the file manager and locate the archive file.
Then, right-click on the file and select “Extract Here.” This will extract your files into a new sub-directory with the same name as the archive file.
2. Unzip All Files in a Different Directory
First, open the file manager and locate the archived file.
Then, right-click on the file and select the “Extract To” option. It will open a new dialogue box.
Select the path to which you want to unzip all the files from the left pane and click on “Select.”
All your files will be extracted in a new sub-directory with the same name as the archived file.
3. Unzip Only Selected Files
First, open the file manager and locate the archive file. Then, double-click on the file. This will open the Archive Manager.
Select multiple files by holding down the “CTRL” key while clicking on the file names you want to select. Then, click on “Extract” in the top left corner.
In the new dialogue box, choose the path and click on “Select” in the top right corner.
All your files will be extracted in a new sub-directory with the same name as the archived file.
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, zip and unzip are 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 the zip command is as follows:
zip
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 -Z flag, 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 -d flag 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 .zip which 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
Guide to Zip and Unzip Files & Directory in Linux
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