zip and unzip is a compression and file packaging utility for Linux, Unix, Windows & Mac OS X.
Its useful for packaging/compressing a set of normal files, one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity) and stores in a single zip file.
This save disk space by temporarily compressing unused files or directories.
It will automatically uses the Zip64 extensions when files larger than 4 GB are added to an archive, or when the number of entries in the archive will exceed about 64K.
The unzip program used to unpacks zip archives. We can create a entire directory structure zip archive with a single command.
Compression ratios of 2:1 to 3:1 are common for text files.
Both utilities are available in all major Linux distribution by default but not installed.
Run the following commands to install zip & unzip utilities in Linux.
For Debian/Ubuntu/Mint: Use APT Package Manager or APT-GET Package Manger to install zip & unzip.
$ sudo apt install zip unzip
For CentOS/RHEL: Use YUM Package Manager to install zip & unzip.
$ sudo yum install zip unzip
For Fedora: Use DNF Package Manager to install zip & unzip.
$ sudo dnf install zip unzip
For openSUSE: Use zypper Package Manager to install zip & unzip.
$ sudo zypper install zip unzip
For Arch Linux: Use pacman Package Manager to install zip & unzip.
$ sudo pacman -S zip unzip
Syntax:
Common syntax for zip and unzip command.
[zip syntax] # zip [.zip file name] [file to be compressed] [zip (Decompress) syntax] # zip -d [.zip file name] [unzip syntax] # unzip [.zip file name]
1) How to create a zip file
The below example zip/compress the test-zip.txt
file into test.zip
file. See the following example.
# zip test.zip test-zip.txt adding: test-zip.txt (stored 0%)
We are going to check whether the mentioned file was compressed or not.
# ll -h
total 48K
lrwxrwxrwx. 1 root root 31 Dec 7 09:41 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx. 1 root root 38 Dec 7 09:42 s-link-folder -> /links/soft-link/test-soft-link-folder
-rw-r--r--. 1 root root 200 Dec 27 11:58 test.zip
-rw-r--r--. 1 root root 26 Dec 27 11:55 test-zip.txt
2) How to View list of files in ZIP archive without extracting
The below command is help us to How to View list of files in ZIP archive without extracting.
# less test.zip Archive: test.zip Zip file size: 200 bytes, number of entries: 1 -rw-r--r-- 3.0 unx 26 tx stor 13-Dec-27 11:55 test-zip.txt 1 file, 26 bytes uncompressed, 26 bytes compressed: 0.0%
3) How to unzip/Decompress a file
Use the following unzip command and followed by zip file name to unzip/decompress the file.
# unzip test.zip Archive: test.zip replace test-zip.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: test-zip.txt
The above output asking the question while unzip the file since the source file test-zip.txt
in the same location.
4) How to zip/compress a folder
The below example zip/compress the link
folder to test1.zip
file.
# zip test1.zip links adding: links/ (stored 0%)
Output:
# ll -h
total 56K
-rw-r--r--. 1 root root 26 Dec 27 12:25 liks
lrwxrwxrwx. 1 root root 31 Dec 7 09:41 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx. 1 root root 38 Dec 7 09:42 s-link-folder -> /links/soft-link/test-soft-link-folder
-rw-r--r--. 1 root root 162 Dec 27 12:29 test1.zip
-rw-r--r--. 1 root root 200 Dec 27 11:58 test.zip
-rw-r--r--. 1 root root 26 Dec 27 11:55 test-zip.txt
You can re check whether the link
folder is there in the zip archive or not by using less
command.
# less test1.zip Archive: test1.zip Zip file size: 162 bytes, number of entries: 1 drwxr-xr-x 3.0 unx 0 bx stor 13-Dec-27 12:26 links/ 1 file, 0 bytes uncompressed, 0 bytes compressed: 0.0%
The output shows only one file because it doesn’t compress the file/folders recursively.
5) How to zip a file/folder recursively
The below example zip/compress the link
folder recursively into test2.zip
file.
# zip -r test2.zip links adding: links/ (stored 0%) adding: links/soft-link/ (stored 0%) adding: links/soft-link/test-soft-link (stored 0%) adding: links/soft-link/test-zip.txt (stored 0%) adding: links/soft-link/test-soft-link-folder/ (stored 0%) adding: links/test-zip.txt (stored 0%)
The above output clearly says that it compressed files and folders recursively.
Once again you can cross check the compressed file contents without extracting.
# less test2.zip Archive: test2.zip Zip file size: 1130 bytes, number of entries: 6 drwxr-xr-x 3.0 unx 0 bx stor 13-Dec-27 12:26 links/ drwxr-xr-x 3.0 unx 0 bx stor 13-Dec-27 12:27 links/soft-link/ -rw-r--r-- 3.0 unx 16 tx stor 13-Dec-07 09:38 links/soft-link/test-soft-link -rw-r--r-- 3.0 unx 26 tx stor 13-Dec-27 12:27 links/soft-link/test-zip.txt drwxr-xr-x 3.0 unx 0 bx stor 13-Dec-07 09:39 links/soft-link/test-soft-link-folder/ -rw-r--r-- 3.0 unx 26 tx stor 13-Dec-27 12:26 links/test-zip.txt 6 files, 68 bytes uncompressed, 68 bytes compressed: 0.0%
The output clearly shows the file and folders recursively compressed.
6) How to zip a file from different location
The below example zip/compress a test1-zip.txt
, test2-zip.txt
and soft-link
folder recursively into test3.zip
file.
# zip -r test3.zip test1-zip.txt test2-zip.txt /home/mageshm/links/soft-link adding: test1-zip.txt (stored 0%) adding: test2-zip.txt (stored 0%) adding: home/mageshm/links/soft-link/ (stored 0%) adding: home/mageshm/links/soft-link/test-soft-link (stored 0%) adding: home/mageshm/links/soft-link/test-zip.txt (stored 0%) adding: home/mageshm/links/soft-link/test-soft-link-folder/ (stored 0%)
Once again confirm whether the file compressed or not.
# ll -h
total 68K
-rw-r--r--. 1 root root 26 Dec 27 12:25 liks
lrwxrwxrwx. 1 root root 31 Dec 7 09:41 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx. 1 root root 38 Dec 7 09:42 s-link-folder -> /links/soft-link/test-soft-link-folder
-rw-r--r--. 1 root root 162 Dec 27 12:29 test1.zip
-rw-r--r--. 1 root root 26 Dec 27 11:55 test1-zip.txt
-rw-r--r--. 1 root root 1.2K Dec 27 12:37 test2.zip
-rw-r--r--. 1 root root 26 Dec 27 12:43 test2-zip.txt
-rw-r--r--. 1 root root 1.3K Dec 27 12:53 test3.zip
-rw-r--r--. 1 root root 200 Dec 27 11:58 test.zip
Cross check once the zip/compressed file contents without extracting.
# less test3.zip Archive: test3.zip Zip file size: 1264 bytes, number of entries: 6 -rw-r--r-- 3.0 unx 26 tx stor 13-Dec-27 11:55 test1-zip.txt -rw-r--r-- 3.0 unx 26 tx stor 13-Dec-27 12:43 test2-zip.txt drwxr-xr-x 3.0 unx 0 bx stor 13-Dec-27 12:27 home/mageshm/links/soft-link/ -rw-r--r-- 3.0 unx 16 tx stor 13-Dec-07 09:38 home/mageshm/links/soft-link/test-soft-link -rw-r--r-- 3.0 unx 26 tx stor 13-Dec-27 12:27 home/mageshm/links/soft-link/test-zip.txt drwxr-xr-x 3.0 unx 0 bx stor 13-Dec-07 09:39 home/mageshm/links/soft-link/test-soft-link-folder/ 6 files, 94 bytes uncompressed, 94 bytes compressed: 0.0%
The output clearly shows the mentioned two files test1-zip.txt
, test2-zip.txt
and soft-link
folder recursively compressed.
Hope this article was useful for you. Kindly provide your valuable feedback/comments in the commenting section.
Hi,
Thanks for giving this nice information about Linux. Its helps me to update my knowledge…Keep on doing this service..!!