Software & Apps Linux 33 33 people found this article helpful Practical Examples of the zip Command There are plenty of things you can do with the Linux zip command by Gary Newell Writer Gary Newell was a freelance contributor, application developer, and software tester with 20+ years in IT, working on Linux, UNIX, and Windows. our editorial process Gary Newell Updated on February 13, 2020 Linux Switching from Windows Tweet Share Email There are a number of different ways to compress files using the Linux command line. This article includes practical examples that show how to use the zip command to compact and organize files within your file system. Zipped files are used when you need to save space and copy large files from one place to another. If you have 10 files that are all 100 megabytes in size and you need to transfer them to an ftp site, the transfer could take a considerable amount of time depending on your processor speed. If you compress all 10 files into a single zipped archive and the compression reduces the file size to 50MB per file, then you only have to transfer half as much data. How to Create an Archive of All the Files in a Folder Imagine you have a folder of songs with the following MP3 files in it: AC/DC Highway to Hell Night Prowler.mp3 Love hungry man.mp3 Get It Hot.mp3 Walk all over you.mp3 Highway to hell.mp3 If you want blood you got it.mp3 Show down in flames.mp3 Touch too much.mp3 Beating around the bush.mp3 Girls Got Rhythm.mp3 This simple Linux command that illustrates how to create an archive of all the files in the current folder called ACDC_Highway_to_Hell.zip: zip ACDC_Highway_to_Hell * Text scrolls up the screen showing the files as they are being added. How to Include Hidden Files in an Archive The previous command is fine for archiving all the files in a folder but it only includes files that aren't hidden. It's not always this simple. Imagine you wanted to zip your home folder so that you can back it up to a USB drive or external hard drive. Your home folder includes hidden files. To compress all the files including the hidden files in a folder, run the following command: zip home * .* This creates a file called home.zip with all the files within the home folder. (You must be in the home folder for this to work). The problem with this command is that it only includes the files in the home folder and not the folders, which brings us to the next example. How to Archive All Files and Subfolders in a Zip File To include all the files and subfolders within an archive, run the following command: zip -r home . How to Add New Files to an Existing Zipped Archive If you want to add new files to an existing archive or update the files in an archive, use the same name for the archive file when running the zip command. For example, imagine you have a music folder with four albums in it and you create an archive called music.zip to keep as a backup. Now imagine one week later you download two new albums. To add the new albums to the zip file, simply run the same zip command as you did the previous week. To create the original music archive run the following code: zip -r music /home/yourname/music/ To add new files to the archive run the same command again. If the zip file has a list of files in it and one of the files on the disk has changed, then the amended file is updated in the zip file. How to Update the Existing Files in a Zipped Archive If you have a zip file that is supposed to contain the same file names every time and you want to update that file with any changes that have been made to those files then the -f switch helps you do this. For example, imagine you have a zipped file with the following files: /home/yourname/documents/file1 /home/yourname/documents/file2 /home/yourname/documents/file3 /home/yourname/documents/file4 /home/yourname/documents/file5 /home/yourname/documents/file6 Now imagine that during the week, you added two new files and amended two files so that the folder /home/yourname/documents now looks like this: /home/yourname/documents/file1 /home/yourname/documents/file2 /home/yourname/documents/file3 /home/yourname/documents/file4 (updated) /home/yourname/documents/file5 (updated) /home/yourname/documents/file6 /home/yourname/documents/file7 /home/yourname/documents/file8 When you run the following command the zip file will contain the updated files (file4 and file5) but file7 and file8 will not be added. zip zipfilename -f -r /home/yourname/documents How to Delete Files From a Zipped Archive So you created a massive zip file with hundreds of files and now realize that there are four or five files in the zip file that you don't need there. Without having to zip all those files again, you can just run the zip command with the -d switch as follows: zip zipfilename -d [name of file in archive] For example, if you have a file in the archive with the name home/documents/test.txt, you delete it with this command: zip zipfilename -d home/documents/test.txt How to Copy Files From One Zip File to Another If you have files in one zip file and you want to copy them to another zip file without extracting them first and rezipping them, use the -u switch. Assume you have a zip file called "variousmusic.zip" with music from various artists, one of which is AC/DC. You can copy the AC/DC songs out of the variousmusic.zip file into your ACDC.zip file using the following command: zip variousmusic.zip -U --out ACDC.zip "Back_In_Black.mp3" The above command copies the file "Back_In_Black.mp3" from variousmusic.zip to ACDC.zip. If the zip file you are copying to doesn't exist, it is created. How to Use Pattern Matching and Piping to Create an Archive The next switch is a really useful one because it lets you use the output of other commands to insert files into your zip file. Assume you want to create a file called lovesongs.zip, which contains every song which has the word love in the title. To find the files with love in the title you can use the following command: find /home/yourname/Music -name *love* The above command isn't 100 percent perfect because it picks up words like "clover" as well, but you get the idea. To add all the returned results from the above command to a zip file called lovesongs.zip, run this command: find /home/yourname/Music -name *love* | zip lovesongs.zip -@ How to Create a Split Archive If you are backing up your computer but the only media you have available for backing up to is a set of blank DVDs, then you have a choice. You can keep zipping files until the zip file is 4.8 gigabytes and burn the DVD, or you can create something called a split archive which keeps creating new archives in a set after it reaches the limit that you specify. For example: zip mymusic.zip -r /home/myfolder/Music -s 670m How to Customize the Progress Report of the Zipping Process There are various ways to customize the output that appears while zipping is in progress. The switches available are as follows: -db = displays how many bytes have been zipped and how many are left to go-dc = displays a count of files zipped and how many are left to go-dd = displays dots for every 10MB of file that has zipped-ds = sets how often dots appear-du = displays the uncompressed size of each file For example: zip myzipfilename.zip -dc -r /home/music How to Fix a Zip File If you have a zip archive that is broken, you can try and fix it using the -F command and if that fails, the FF command. This is useful if you have created a split archive using the -s switch, and you lost one of the archive files. For example, try this one first: zip -F myfilename.zip --out myfixedfilename.zip and then zip -FF myfilename.zip --out myfixedfilename.zip How to Encrypt an Archive If you have sensitive information that you want to store in a zip file, use the -e command to encrypt it. You are asked to enter a password and to repeat the password. For example: zip myfilename.zip -r /home/wikileaks -e How to Show What Will Be Zipped If you know you are going to be creating a large archive, make sure that the correct files are going to be added to the zip file. You can see the expected results of a zip command by specifying the -sf switch. For example: zip myfilename.zip -r /home/music/ -sf How to Test an Archive After backing up files to a zip file, it is tempting to save disk space by deleting the original files. Before you do that, it's a good idea to test the zip file works properly. You can use the -T switch to test that the zip file is valid. For example: zip myfilename.zip -T Output from this command when an archive is invalid may look something like: zip warning: missing end signature--probably not a zip file Remember you can try the -F command to fix broken zip files. It is worth noting that the -T can produce false positives in that it says a zip file is corrupt even though when you open it, you can extract all the files. How to Exclude Files Sometimes you want to exclude certain files from a zip file. For example, if you copy the files from your phone or digital camera, you have a mixture of videos and images. You might wish to zip up the photos to photos.zip and videos to videos.zip. Here is one way to exclude the videos when creating photos.zip zip photos.zip -r /home/photos/ -x *.mp4 How to Specify Compression Level When you compress files into a zip file, the system decides whether to compress the file or just store it. MP3 files, for example, are already compressed, so there is little point in compressing them further; they are usually stored as is within a zip file. You can, however, specify a compression level between 0 and 9 to compress a file further. This takes longer to do, but it can make significant space savings. zip myfiles.zip -r /home -5 Was this page helpful? Thanks for letting us know! Get the Latest Tech News Delivered Every Day Email Address Sign up There was an error. Please try again. You're in! Thanks for signing up. There was an error. Please try again. Thank you for signing up. Tell us why! Other Not enough details Hard to understand Submit