How to zip/unzip a file on Linux?Posted by Joe / April 12th, 2017 / No responses
On Windows, you might have seen some visual file compression and decompression tools like WinZip, WinRar, and 7Zip. Linux goes a step further by providing 2 ways to zip and unzip a file- through GUI-based tools, and through command line.
Here, I am going to discuss some basic file compression and decompression operations using the Linux command line. You will find these commands useful on different occasions when you have to do more than just a regular file compression or decompression.
1. Compressing file on Linux using command line
A. Navigate to the directory which you want to compress and type in the following command
Zip filename *
PS: filename is the name you want to give to your zip file. Suppose I want to zip the contents of a folder named ‘Sample’ which on Desktop directory. I will first move to the Directory where “Sample” is existing using “cd Desktop/Sample”
Suppose I want to give the zip file name as “Test”
I will use the following command to zip the file here:
zip Test *
B. If you want to zip an entire directory with all the folders and subfolders inside the directory you can use the following command:
Zip -r filename *
In my case, it would be
zip -r Test *
2. Unzipping a single file into current folder
To unzip a file in the current folder the basic command line goes as follows:
Suppose I want to unzip a zipped up file named ‘Sample’ on the Desktop then I will first navigate to the Desktop directory and then use the following command:
3. Unzipping multiple files using a single command line
Some articles on the internet say you can unzip multiple files in a directory at once using the following command Syntax:
unzip filename1 filename2 filename3
For example, I have three zipped up files named “Trash”, “Rag” and “Town” on Desktop/Sample directory which I want to decompress using a single command line.
To unzip these files all I need is navigate to the file directory and enter the following:
unzip “Trash.zip” “Rag.zip” “Town.zip”
However, When I tried the same I receive an error and it did not work for me. I was good using an alternative option of:
Caution: This command will unzip every file in the current folder.
4. Unzipping a file but excluding certain files in it
If you want to unzip a file, but do not want to extract certain files from it, You can use the following command syntax:
unzip filename.zip -x filenottoextract.zip
For example, to extract all files from ‘Rag.zip’ except a file named ‘software.txt’, I would use the following command line:
unzip Rag.zip-x “software.txt”
5. Unzipping a file to a different directory
If you want to unzip you zipped up file to a directory other than the current one, you can use the following command syntax:
unzip filename.zip -d path/to/extract/the/file
For example, to unzip Trash.zip to /home/collection/useful/Trash I will use the following command line:
unzip Trash.zip -d /home/collection/useful/Trash
6. Unzipping a zip file to current directory without creating folders
If you wish to unzip all the files from your zip file but it consist of several sub-folders inside it, and you want to extract files inside all of them in one location without creating the subfolders, you can use the following command syntax:
unzip -j filename.zip
Just move to the file directory first and type the above command with correct file name.
7. Unzip a password protected zip file
To unzip a password protected zip file you have to perform the password authentication in the same command you use to unzip a regular zip file. The syntax goes like this:
unzip -P <password> filename.zip
Suppose I want to unzip a file which has been protected by a password “xyz@123″, I would use the following command syntax.
unzip -P xyz@123 filename.zip
There are hundreds of command syntax to handle a file compression and decompression on Linux system. However, I have explained some really useful command lines that you can use to make your life easier with the Linux file compression and decompression operations. After getting used to with these command lines, who knows, you might find it easier and quicker than the GUI tools.