ZIP command in Linux with Pratical examples
ZIP is a compression and file packaging utility for Unix. The Files are stored in ".zip" extensions.
- zip is used to compress the files to reduce file size across all the oS flavors (Unix, Linux, Windows).
- The zip program puts 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). An entire directory structure can be packed into a zip archive with a single command.
How to create a zip file
Syntax :
zip [options] zipfile file
Creating a zip file:
[root@Test-Server-devopszones] #zip gluster.zip gluster.sh
adding: gluster.sh (deflated 68%)
[root@Test-Server-devopszones] #ls gluster.zip
gluster.zip
[root@Test-Server-devopszones] #
How to Extract files from zip file
Unzip will list, test, or extract files from a ZIP archive, commonly found on Unix systems. The default behavior (with no options) is to extract into the current directory (and sub-directories below it) all files from the specified ZIP archive.
[root@Test-Server-devopszones] #unzip gluster.zip
Archive: gluster.zip
inflating: gluster.sh
[root@Test-Server-devopszones] #
[root@Test-Server-devopszones] #zipinfo -1 gluster.zip
gluster.sh
[root@Test-Server-devopszones] #
[root@Test-Server-devopszones] #unzip -Z1 gluster.zip
gluster.sh
[root@Test-Server-devopszones] #
[root@Test-Server-devopszones] #zip -u gluster.zip ram.txt
adding: ram.txt (stored 0%)
[root@Test-Server-devopszones] #zipinfo -1 gluster.zip
gluster.sh
ram.txt
[root@Test-Server-devopszones] #
This option removes the file from the zip archive. After creating a zip file, you can remove a file from the archive using the -d option.
[root@Test-Server-devopszones] #zip -d gluster.zip ram.txt
deleting: ram.txt
[root@Test-Server-devopszones] #zipinfo -1 gluster.zip
gluster.sh
[root@Test-Server-devopszones] #
To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.
Syntax
zip –r filename.zip directory_name
[root@Test-Server-devopszones] #zip -r scripts.zip scripts
adding: scripts/ (stored 0%)
adding: scripts/firewall.sh (deflated 62%)
adding: scripts/gluster.sh (deflated 68%)
[root@Test-Server-devopszones] #
How to delete Original file After Zipping
This Option Deletes the original files after zipping. No deletions are done until zip has created the archive without error.
[root@Test-Server-devopszones] #ls -la scripts/
total 20
drwxr-xr-x. 2 root root 43 Dec 18 05:03 .
dr-xr-x---. 8 root root 4096 Dec 18 05:10 ..
-rw-r--r--. 1 root root 627 Nov 25 18:14 firewall.sh
-rw-r--r--. 1 root root 11726 Nov 25 18:14 gluster.sh
[root@Test-Server-devopszones] #zip -rm scripts.zip scripts
adding: scripts/ (stored 0%)
adding: scripts/firewall.sh (deflated 62%)
adding: scripts/gluster.sh (deflated 68%)
[root@Test-Server-devopszones] #ls -la scripts/
ls: cannot access scripts/: No such file or directory
[root@Test-Server-devopszones] #
This Option Exclude the files in creating the zip. In the following example we are excluding "scripts/firewall.sh" from zip file.
[root@Test-Server-devopszones] #ls -la scripts/
total 28
drwxr-xr-x. 2 root root 62 Dec 18 06:09 .
dr-xr-x---. 8 root root 4096 Dec 18 06:22 ..
-rw-r--r--. 1 root root 627 Nov 25 18:14 firewall.sh
-rw-r--r--. 1 root root 11726 Nov 25 18:14 gluster.sh
-rw-r--r--. 1 root root 4289 Dec 18 06:09 scripts.zip
[root@Test-Server-devopszones] #zip -rv scripts.zip scripts/ -x "scripts/firewall.sh"
adding: scripts/ (in=0) (out=0) (stored 0%)
adding: scripts/gluster.sh (in=11726) (out=3730) (deflated 68%)
adding: scripts/scripts.zip (in=4289) (out=4289) (stored 0%)
total bytes=16015, compressed=8019 -> 50% savings
[root@Test-Server-devopszones] #
How to Exclude files with Wildcard from a zip File
In the following example we are excluding all the files with "*.zip" extension.
[root@Test-Server-devopszones] #zip -rv scripts.zip scripts/ -x "scripts/firewall.sh" -x \*.zip
adding: scripts/ (in=0) (out=0) (stored 0%)
adding: scripts/gluster.sh (in=11726) (out=3730) (deflated 68%)
total bytes=11726, compressed=3730 -> 68% savings
[root@Test-Server-devopszones] #
How to Exclude multiple files a zip File
In the following example we are excluding all the files with "*.zip" extension.
[root@Test-Server-devopszones] #zip -rv scripts.zip scripts/ -x \*.zip
adding: scripts/ (in=0) (out=0) (stored 0%)
adding: scripts/firewall.sh (in=627) (out=239) (deflated 62%)
adding: scripts/gluster.sh (in=11726) (out=3730) (deflated 68%)
total bytes=12353, compressed=3969 -> 68% savings
[root@Test-Server-devopszones] #
How to exclude a directory from zip File
[root@Test-Server-devopszones] #rm -f scripts.zip
[root@Test-Server-devopszones] #zip -rv scripts.zip scripts/ -x "scripts/testdir/"
adding: scripts/ (in=0) (out=0) (stored 0%)
adding: scripts/firewall.sh (in=627) (out=239) (deflated 62%)
adding: scripts/gluster.sh (in=11726) (out=3730) (deflated 68%)
adding: scripts/scripts.zip (in=4289) (out=4289) (stored 0%)
total bytes=16642, compressed=8258 -> 50% savings
[root@Test-Server-devopszones] #
How to Print Information While creating a zip File
This option enables the display of a progress indicator during compression and requests verbose diagnostic info about zip file structure oddities.
[root@Test-Server-devopszones] #zip -v scripts.zip *.sh
adding: firewall.sh (in=627) (out=239) (deflated 62%)
adding: gluster.sh (in=11726) (out=3730) (deflated 68%)
total bytes=12353, compressed=3969 -> 68% savings
[root@Test-Server-devopszones] #
No comments