Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I Zip/Unzip on the Unix Command Line
Zipping and unzipping files is a fundamental practice in Unix/Linux systems for file compression and archiving. We compress files to save disk space and bundle multiple files into a single archive for easier transfer and storage.
When working with compressed archives, understanding both compression and extraction commands is essential for effective file management in Unix environments.
Installing zip and unzip Commands
First, ensure that the zip and unzip utilities are installed on your system:
sudo apt install zip unzip
Basic zip and unzip Operations
Let's start with creating some sample files to demonstrate the compression process:
touch 1.txt 2.txt 3.txt 4.txt ls
1.txt 2.txt 3.txt 4.txt
Now compress all text files into a single archive named all.zip:
zip all *.txt
adding: 1.txt (stored 0%) adding: 2.txt (stored 0%) adding: 3.txt (stored 0%) adding: 4.txt (stored 0%)
The archive all.zip is now created. To extract the files, use the unzip command:
rm *.txt unzip all.zip
Archive: all.zip extracting: 1.txt extracting: 2.txt extracting: 3.txt extracting: 4.txt
Extract to Target Directory
To avoid cluttering the current directory, extract files to a specific target directory using the -d option:
unzip -q all.zip -d new-all ls new-all/
1.txt 2.txt 3.txt 4.txt
The -q flag suppresses the verbose output during extraction.
Exclude Files During Compression
To exclude specific files from compression, use the -x option:
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%)
Verify the exclusion by extracting to a new directory:
unzip -q all.zip -d excluded-demo ls excluded-demo/
1.txt 2.txt 3.txt
List Archive Contents
To view the contents of a zip archive without extracting it, use the -l option:
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 Protection
Create password-protected archives using the -e (encrypt) and -r (recursive) options:
zip -e -r password-protected new-all/
Enter password: Verify password: adding: new-all/ (stored 0%) adding: new-all/1.txt (stored 0%) adding: new-all/2.txt (stored 0%) adding: new-all/3.txt (stored 0%)
When extracting password-protected archives, you'll be prompted for the password:
unzip password-protected.zip
Archive: password-protected.zip [password-protected.zip] new-all/1.txt password: extracting: new-all/1.txt
Common zip and unzip Options
| Command | Option | Description |
|---|---|---|
| zip | -r | Recursively compress directories |
| zip | -e | Encrypt with password |
| zip | -x | Exclude specified files |
| unzip | -d | Extract to specified directory |
| unzip | -l | List archive contents |
| unzip | -q | Quiet mode (suppress output) |
Conclusion
The zip and unzip commands provide essential file compression and extraction capabilities in Unix systems. These tools support various options including password protection, selective file exclusion, and directory-specific extraction, making them versatile for different archiving scenarios.
