
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Zip and Unzip Files on the Unix Command Line
Introduction
Zipping and unzipping a file is a very common practice in Linux. We need to zip many files into one file for the following reasons.
It saves disk space in the system.
We can keep multiple files inside one file. This also helps to copy this zipped file to another system.
When we have one zipped file, we should also know how to unzip it to get all the required files. So, zipping and unzipping commands are very important in Linux. In this article, we are going to learn on various commands for zipping and then unzipping.
Using the Basic zip and unzip Command
First of all, "zip" and "unzip" commands should be present in Linux. If these commands are not present then install using the below command.
$ sudo apt install zip $ sudo apt install unzip
Now, let us create 4 text files.
$ touch 1.txt 2.txt 3.txt 4.txt $ ls 1.txt 2.txt 3.txt 4.txt
Zip all 4 files into one file "all"
$ zip all *.txt adding: 1.txt (stored 0%) adding: 2.txt (stored 0%) adding: 3.txt (stored 0%) adding: 4.txt (stored 0%)
As we can see all.zip file is created which contains 1.txt , 2.txt , 3.txt , 4.txt .
$ ls 1.txt 2.txt 3.txt 4.txt all.zip
Let us now delete all 4 files and unzip all.zip. We can see all 4 text files.
$ rm *.txt $ unzip all.zip Archive: all.zip extracting: 1.txt extracting: 2.txt extracting: 3.txt extracting: 4.txt $ ls 1.txt 2.txt 3.txt 4.txt all.zip
Unzip Files to a Target Directory
In the previous example, we have seen that all text files are getting extracted in the current path. It's always recommendable to unzip files into a different directory so that extracted files are not mixed up with other files in the current path.
We can use the below command to extract "all.zip" to the directory "new-all"
$ unzip -q all.zip -d new-all $ ls new-all/ 1.txt 2.txt 3.txt 4.txt
Exclude Particular Files While Zipping
In some scenarios, if we need to exclude some files to be zipped we can use the below command.
$ zip all 1.txt 2.txt 3.txt -x 4.txt updating: 1.txt (stored 0%) updating: 2.txt (stored 0%) updating: 3.txt (stored 0%)
From the output, we can see 4.txt is not included at all.zip.
Let us unzip "all.zip".
$ unzip -q all.zip -d 4.txt-not-there $ ls 4.txt-not-there/ 1.txt 2.txt 3.txt
From the above output, we can see "4.txt" is not there.
Find all Files Inside the Zipped File Without Unzipping it
In some scenarios, if we need to see what files are there inside zipped the file, then we can use the below command.
$ unzip -l all.zip Archive: all.zip Length Date Time Name --------- ---------- ----- ---- 0 2016-02-11 22:25 1.txt 0 2016-02-11 22:25 2.txt 0 2016-02-11 22:25 3.txt --------- ------- 0 3 files
Password Protected Your Zipped File
In some scenarios, if we need to protect the zipped file using a password, we can use the below command.
$ zip -e -r password-protected new-all/ Enter password: Verify password: adding: new-all/ (stored 0%) adding: new-all/4.txt (stored 0%) adding: new-all/2.txt (stored 0%) adding: new-all/3.txt (stored 0%) adding: new-all/1.txt (stored 0%)
As we can see the password is prompted to the user and "password-protected.zip" is created.
$ ls 1.txt 2.txt 3.txt 4.txt 4.txt-not-there all.zip new-all password-protected.zip
Now let us unzip "password-protected.zip" with the correct password.
$ unzip password-protected.zip Archive: password-protected.zip [password-protected.zip] new-all/4.txt password: replace new-all/4.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/4.txt replace new-all/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/2.txt replace new-all/3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/3.txt replace new-all/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/1.txt
Now try the same "unzip" command with the wrong password.
$ unzip password-protected.zip Archive: password-protected.zip [password-protected.zip] new-all/4.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/4.txt incorrect password [password-protected.zip] new-all/2.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/2.txt incorrect password [password-protected.zip] new-all/3.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/3.txt incorrect password [password-protected.zip] new-all/1.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/1.txt incorrect password
We can see it's not successful.
Conclusion
From this article, we have learned how to use "zip" and "unzip" commands. Also using various arguments to perform password-protected zipped file, unzip the zipped file to a target directory and exclude some files while zipping.