The zip command in Linux is a utility used for packaging a group of files into a zip archive. The zip command can also be used to compress files, making them take up less space on your computer’s hard drive. In addition, the zip command can be used to encrypt files, making it more difficult for unauthorized users to access the contents of the zip archive.
To unzip a zip archive, you can use the unzip command. This command will decompress the files in the zip archive, allowing you to access them.
Zip Command Examples
Go through the below real examples of Linux zip command.
- Zip all files in a directory
This command will create a zip of all files in the /backup directory. I will not archive files under the sub directories recursively.
zip backup.zip /backup/*Output:adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/httpd/ (stored 0%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%)
- Zip files with wildcard
Use Linux wildcards to archive files of specific extensions only. Like backup only .log extension files in a directory.
zip backup.zip /backup/*.logOutput:adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%) adding: backup/pm-powersave.log (deflated 15%) adding: backup/wpa_supplicant.log (stored 0%) adding: backup/Xorg.0.log (deflated 83%) adding: backup/Xorg.9.log (deflated 83%) adding: backup/yum.log (deflated 77%)
- Zip files recursively
The below command will create an archive recursively with files, directories, and its sub-directories as well. It also maintains the directory structure as source files.
zip -r backup.zip /backup
- Create password protected zip
Sometimes we need to create a password-protected archive. The zip command provides you an option to make password-protected archive files. To create password-protected archive use -e option. This will prompt for a password and confirm the password.
zip -e backup.zip /backup/*.logOutput:Enter password: Verify password: adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%)
- Zip with compression levels
You can also define the compression level with the zip command. There are 10 levels of compression, which range from 0 to 9.
- -6 is used as default compression level.
- -0 is used for the lowest level compression.
- -9 is used for the highest level compression
zip -9 high-compressed-file.zip /backup/*
zip -0 lowest-compressed-file.zip /backup/*
Check differences between a compressed file
ls -lh lowest-compressed-file.zip high-compressed-file.zipOutput:-rw-r--r--. 1 root root 50K Apr 11 14:14 high-compressed-file.zip -rw-r--r--. 1 root root 447K Apr 11 14:14 lowest-compressed-file.zip
You can see the difference between both file sizes.
- List content of zip file
-lswitch with unzip command to list only files inside a zip archive without decompressing it.
unzip -l backup.zipOutput:Archive: backup.zip Length Date Time Name --------- ---------- ----- ---- 140 04-11-2013 14:07 backup/anaconda.ifcfg.log 11153 04-11-2013 14:07 backup/anaconda.log 15446 04-11-2013 14:07 backup/anaconda.program.log 136167 04-11-2013 14:07 backup/anaconda.storage.log 2722 04-11-2013 14:07 backup/boot.log 211614 04-11-2013 14:07 backup/dracut.log 0 04-11-2013 14:08 backup/httpd/ 1382 04-11-2013 14:07 backup/kadmind.log 1248 04-11-2013 14:07 backup/krb5kdc.log 6485 04-11-2013 14:07 backup/mysqld.log 87 04-11-2013 14:07 backup/pm-powersave.log 0 04-11-2013 14:07 backup/wpa_supplicant.log 30186 04-11-2013 14:07 backup/Xorg.0.log 31094 04-11-2013 14:07 backup/Xorg.9.log 6739 04-11-2013 14:07 backup/yum.log --------- ------- 454463 15 files
- Extract a zip file.
The unzip command is used to extract a zip file. Use the below command to simply extract a zip file.
- Check an archive file
Use -t to check and archive files. This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum).
unzip -t backup.zipOutput:Archive: backup-11Apr2013.zip testing: backup/anaconda.ifcfg.log OK testing: backup/anaconda.log OK testing: backup/anaconda.program.log OK testing: backup/anaconda.storage.log OK testing: backup/boot.log OK testing: backup/dracut.log OK testing: backup/httpd/ OK testing: backup/kadmind.log OK testing: backup/krb5kdc.log OK testing: backup/mysqld.log OK testing: backup/pm-powersave.log OK testing: backup/wpa_supplicant.log OK testing: backup/Xorg.0.log OK testing: backup/Xorg.9.log OK testing: backup/yum.log OK No errors detected in compressed data of backup.zip.
We’ve shown you how to zip files in Linux using the terminal. This is a handy skill that can come in handy when you need to send someone a compressed file or save storage space on your computer. Also, the Unzipping an archive is easy and we’ll show you how to do it quickly and easily.
Thank you, I’m a novice in Linux, it was really helpful!
> zip 9 high-compressed-file.zip /backup/*
Mhmm I think it is :
> zip -9 high-compressed-file.zip /backup/*