Master The ZIP Command In Linux With 5 Examples

Table of Contents

Need to learn how to use the zip command in Linux? Countless lovely things can be stored in .zip files. From operating systems to awesome media files, zip archives can store backups, pictures, programs, and even movies. You can especially add a password to increase privacy. Capable of storing up to 4gb of data successfully, while also offering a measure of compression. First released February 14, 1989, as a result of a lawsuit. You can see Linux zip files have come a long way, and they are still relevant today.

In the past, I have used .zip files to transfer everything from web applications to wedding albums. So .zip files are great for storing raw data, keeping your applications in order, and safe to send to your friends and family. You may know that .zip files can be used to pirate media. Don’t let that make you think they are bad or illegal by default. They are what you make of them. You can use .zip files on most operating systems, both paid and free. We will show you how to zip a file, view its archive while still compressed, specify compression level, and how to unzip them. Now, let’s dive a little deeper, to see what this .zip file business is all about.

INSTALLATION

To install the zip command in Linux, you will have to use your distribution package manager. We will cover 4 distributions today. Debian, Arch, openSUSE, and RHEL. This includes every distribution that are based on them.

Debian/Ubuntu

				
					sudo apt install zip unzip
				
			

Arch/Manjaro

				
					sudo pacman -S zip unzip
				
			

RHEL/Fedora

				
					sudo yum install zip unzip
				
			

BASIC ZIP COMMAND IN LINUX USAGE

First thing to learn is, how to zip a file. To make this happen, you only need one more thing. Files of some sort to archive. We will start with an image today. Make sure you are in the same directory as your file before running this command. Try using a copy of a file you already have. Then use that in place of image-1.png.

				
					zip image-1.zip image-1.png
				
			

Notice unlike many applications, the zip command in Linux uses a destination file after the command, then the source file. However counterintuitive, remember this, so you don’t run into issues. Next, we will zip a directory with many files in it.

				
					zip -r docs.zip ~/Documents
				
			

Here you can see we used the -r flag. This is so that everything in the directory will be recursively compressed into your destination .zip file. If you skip using this flag, you will have an empty zipped directory. Also, you may see that we are using the path to the source directory. This is so that you can execute this from any directory you wish the .zip file to reside in. So, if you run this command from a backup directory like /home/user/backup, then docs.zip will be created there. All ready to rsync to your cloud backup, saving bandwidth. As your files will be deflated for transit.

COMPRESSION LEVEL

You may want to specify the compression level for your files or directories. You can do this by assigning a value from -0 all the way up to -9. Giving you one through ten values to work with.

				
					


zip -7 document.zip document.txt
				
			

This is very helpful when you have a directory that you would like to compress to a particular value. Here we will use the -r flag along with the compression value -4.

				
					


zip -4 -r docs.zip ~/Documents
				
			

ENCRYPTION

The cool thing is Linux zip files can be password protected to add that privacy you might be looking for.

				
					zip -e document.zip document.txt
				
			

You will be prompted to add a password. You will then be asked to verify. Do not misplace this password, or this copy with not be retrievable. You can accomplish this with directories as well by using the -r flag in unison with the -e flag.

				
					zip -er docs.zip ~/Documents
				
			

VIEW ZIPPED CONTENTS

Now that your files and directories are compressed, how do we see them, or are they locked away forever? We have several ways to view zipped content. We can use zless, zmore or zcat on a single file that is zipped, Vim a text editor, works well on directories. Let’s jump right into it.

				
					zless document.zip
				
			

Notice that you can scroll throughout a text document, for your convenience. You will notice that you need to exit this program. All you need to do is type q for quit.

				
					q
				
			

Let’s see what zmore has in store for us.

				
					zmore document.zip
				
			

Notice that you just get a print-out in your terminal, allowing you to see it. Now we will be checking out a similar program called zcat.

				
					zcat document.zip
				
			
zip-zcat

You will receive another print-out of the file in the terminal. Making the print-out identical to zmore. Now, let’s see what our buddy Vim can do about seeing files in their directories.

				
					vi docs.zip
				
			

Notice you can see the directory and file contents. You may select any text-based file by hovering your cursor over it and pressing enter.

Then you will be able to edit the file by typing a. This will insert text after the courser.

				
					a
				
			

Then you can save changes and close the text document by typing ZZ. Alternatively, you can use :q to quit without saving.

				
					ZZ
				
			

This will bring you back to the directories and their contents. You will need to type :q to exit back to the command line.

				
					:q
				
			

UNZIP A FILE ON LINUX

Finally, we will show you how to unzip a file on linux!

				
					unzip document.zip
				
			

Presto, you have a decompressed copy of your original file. This can also be accomplished with directories with the same command, only using a directory based .zip instead.

				
					unzip docs.zip
				
			

Presto, you have a decompressed copy of your original file. This can also be accomplished with directories with the same command, only using a directory based .zip instead.

Now that you have a basic overview, we hope that Linux zip files are no more worrisome than working with any other file type. Remember to only use your new powers for good. Don’t be a pirate, make something cool and send it to your friends. Be it documents, a cool demo recording you want to show off, or just a picture album. Even better, pick up a coding language and send your friend a cool password generator or something. The best part of all of this, is that .zip files are not picky about your operating system. If you have a friend using macOS, then you can send your file with confidence that they will be able to work with it. If you have an acquaintance that uses Windows, once again, you’re in luck. This way you can bring them over to the Dark Side, convert them to Linux distributions. You can help them along by zipping up a virtual disk image all ready to go. Saving them the pain of setting one up from scratch. Till next time, thanks for reading.

Meet the Author

Leave a Reply