The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. Credit: Nikkytok / Tavarius / Shutterstock Zipping files allows you to save a compressed version of a file that might serve as a backup of the original. It also allows you to group a collection of related files into a similarly reduced size file for safekeeping. Zipping a single file If we want to zip a single file, you could use a command like the second of the two commands shown below. The file to be zipped (tips.html) is shown in the first command. $ ls -l tips.html -rw-r--r--. 1 shs shs 79873 Sep 19 2023 tips.html $ zip tips tips.html adding: tips.html (deflated 73%) Notice that the file is deflated by 73%. List it again and you’ll see how much smaller it is than the original file which is still on the system. Note that the file extension “zip” will be added automatically if you don’t include it as the extension for your first argument as in the “zip tips” command above. $ ls -l tips.* -rw-r--r--. 1 shs shs 21713 May 7 10:19 tips.zip -rw-r--r--. 1 shs shs 79873 Sep 19 2023 tips.html The zip command does not remove the original file remains on the system. Zipping a series of files You can zip a group of files into a single zip file as a way to back them up in a compressed format. Note that the compression ratio for each file is displayed in the process. $ zip bin bin/* adding: bin/FindFiles (deflated 54%) adding: bin/shapes (deflated 63%) adding: bin/shapes2 (deflated 62%) adding: bin/shapes3 (deflated 45%) $ ls -l bin.zip -rw-r--r--. 1 shs shs 1765 May 7 10:56 bin.zip Use the -q argument if you prefer to not see the details listed for the files as they are added to the zip file. $ zip -q bin bin/* $ If you are zipping a directory that contains subdirectories, those subdirectories, but not their contents will be added to the zip file unless you add the -r (recursive). Here’s an example: $ zip bin bin/* updating: bin/FindFiles (deflated 54%) updating: bin/shapes (deflated 63%) updating: bin/shapes2 (deflated 62%) updating: bin/shapes3 (deflated 45%) updating: bin/NOTES/ (stored 0%) Here’s an example that adds -r and, as a result, includes the NOTES subdirectory’s files in the bin.zip file is it creating. $ zip -r bin bin/* updating: bin/FindFiles (deflated 54%) updating: bin/shapes (deflated 63%) updating: bin/shapes2 (deflated 62%) updating: bin/shapes3 (deflated 45%) updating: bin/NOTES/ (stored 0%) adding: bin/NOTES/finding_files (deflated 5%) adding: bin/NOTES/shapes_scripts (deflated 35%) Using encryption passwords To add a password that will need to be used to extract the contents of a zip file, use a command like the one shown below. Notice that it prompts twice for the password, though it does not display it. $ zip -e -r bin bin/* Enter password: Verify password: updating: bin/FindFiles (deflated 54%) updating: bin/shapes (deflated 63%) updating: bin/shapes2 (deflated 62%) updating: bin/shapes3 (deflated 45%) updating: bin/NOTES/ (stored 0%) updating: bin/NOTES/finding_files (deflated 5%) updating: bin/NOTES/shapes_scripts (deflated 35%) Extracting file from a zip file To extract the contents of a zip file, you would use the unzip command. Notice that, because the zip file below was encrypted with a password, that password needs to be supplied to extract the contents. $ unzip bin.zip Archive: bin.zip [bin.zip] bin/FindFiles password: inflating: bin/FindFiles inflating: bin/shapes inflating: bin/shapes2 inflating: bin/shapes3 creating: bin/NOTES/ inflating: bin/NOTES/finding_files inflating: bin/NOTES/shapes_scripts If you want to extract the contents of a zip file to a different directory, you don’t need to cd to that directory first. Instead, you can simply add the -d option followed by the target directory to specify the new location. $ unzip bin.zip -d /tmp Archive: bin.zip [bin.zip] bin/FindFiles password: inflating: /tmp/bin/FindFiles inflating: /tmp/bin/shapes inflating: /tmp/bin/shapes2 inflating: /tmp/bin/shapes3 creating: /tmp/bin/NOTES/ inflating: /tmp/bin/NOTES/finding_files inflating: /tmp/bin/NOTES/shapes_scripts You can extract a single file from a zip file if you specify its name as listed in the zip file. Here’s an example command where the original file (maybe it’s been damaged in some way) is replaced after confirming that this is what you want. $ unzip bin.zip 'bin/shapes3' Archive: bin.zip replace bin/shapes3? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: bin/shapes3 Wrap-up Zipping files to preserve them, back them up, extract them and require passwords for extraction are all important things to know when dealing with the zip command. Related content opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux how-to Shredding files on Linux with the shred command The shred command is a good option for removing files from a Linux system in a way that makes them virtually impossible to recover. By Sandra Henry-Stocker Apr 18, 2024 4 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe