Using the Linux Terminal to Zip or Unzip Files

These programs might not come pre-installed on server versions of Linux, though.

Using the Linux Terminal to Zip or Unzip Files

ZIP files are a widely-used way to store files that work on Windows, macOS, and Linux. You can make a zip file or open one using simple commands in the Linux terminal.

How ZIP Archives Work

ZIP files are widely used in the Windows world, they're probably the most popular way to compress files. While Linux users often use .tar.gz and tar.bz2 files, if you're exchanging files with someone using Windows, they'll likely send you a ZIP file. If you need to bundle files and share them with a Windows user, using ZIP is the simplest and most compatible choice for everyone.

Zip, Unzip, and More Tools

You might already know that Linux and similar systems like macOS come with tools for making ZIP files and taking files out of them. These tools are named zip and unzip. But there are also some other helpful tools in this group, like zipcloak, zipdetails, zipsplit, and zipinfo.

We looked at a few Linux versions to see if these tools were included by default. All of them were available in Ubuntu 23.04, 19.04, 18.10, and 18.04. They were also there in Manjaro 18.04. However, in Fedora 29 and CentOS, only zip and unzip were included, and none of the other tools.

To add the things that are missing in Fedora 29, you can use this command:

sudo dnf install perl-IO-Compress

Zip, Unzip, and More Tools

To add the missing parts on CentOS 7, use this command:

sudo yum install perl-IO-Compress

Zip, Unzip, and More Tools

If you're using a different Linux system not mentioned earlier and you don't find all the zip tools, you can use that system's package manager to add the missing ones.

Making a ZIP File Using the zip Command

To make a ZIP file, you have to let zip know what the archive file should be called and which files should be inside it. You don't have to put ".zip" at the end of the archive name, but it won't cause any problems if you do.

To create a file named source_code.zip that holds all the C source code files and header files in the current folder, you can use this command:

zip source_code *.c *.h

Making a ZIP File Using the zip Command

As you add each file, you'll see its name, and it will also display how much it compressed the file.

Making a ZIP File Using the zip Command

When you check the new ZIP archive, you'll notice that zip has automatically added the ".zip" file ending.

ls -l source_code.zip

Making a ZIP File Using the zip Command

If you don't want to see messages from zip while it's making the ZIP file, you can use the -q (quiet) option.

zip -q source_code *.c *.h

Making a ZIP File Using the zip Command

Adding Folders to ZIP Files

To put folders inside the ZIP file, use the -r (which means "go through folders") option and add the name of the folder you want on the same line as the command. To make a ZIP file just like before but also have the "archive" folder inside it, use this command.

zip -r -q source_code archive/ *.c *.h

Adding Folders to ZIP Files

It's a good idea to make ZIP files in a thoughtful way so that the person who opens it can easily find the files. When they unzip it, all the files should be neatly placed in a folder on their computer.

In this command, we're going to save the "work" folder along with all its sub-folders. Remember, this command is run from the main directory, not inside the "work" folder.

zip -r -q source_code work/

Adding Folders to ZIP Files

Adjusting Compression Strength

You can decide how much squeezing happens to the files when they go into the ZIP folder. You can choose a level between 0 and 9, where 0 means no squeezing at all. The more squeezing you choose, the longer it takes to make the ZIP file. For normal-sized ZIP files, the extra time doesn't really matter. Plus, for regular-sized ZIP files, the default squeezing level (which is 6) is usually good enough.

To make zip use a particular squeezing level, just put the number on the command line with a "-" like this:

zip -0 -r -q source_code work/

Adjusting Compression Strength

The usual compression level is 6. You don't have to use the -6 option, but it won't cause any problems if you do.

zip -r -q source_code work/

Adjusting Compression Strength

The maximum compression level is level 9.

zip -9 -r -q source_code work/

Adjusting Compression Strength

When you're putting together these files and folders into an archive, here's what you need to know about compression levels:

• No compression (level 0) doesn't shrink the archive much.

• The default compression (level 6) makes it 400K smaller.

• If you go for the highest compression (level 9), it's just 4K smaller than the default.

Now, this might not sound like a lot, but when you're dealing with archives that have loads of files, like hundreds or even thousands, that tiny bit of extra compression for each file starts adding up. So, you end up saving some worthwhile space.

Securing ZIP Files with Passwords

You can make ZIP files more secure by adding passwords. Just use the -e (encrypt) option, and it will ask you to create a password and confirm it.

zip -e -r -q source_code work/

How to Open a ZIP File Using the unzip Command

To get the files out of a ZIP file, just use the unzip command and tell it the name of the ZIP file. Remember to include ".zip" at the end.

unzip source_code.zip

While the files are being taken out of the ZIP, you'll see them listed on the screen.

It's good to know that ZIP files don't keep track of who owns the files. So, when you extract them, they all belong to the person doing the extracting.

If you prefer not to see the list of files while extracting, you can use the "quiet" option by adding -q to the unzip command.

unzip -q source_code.zip

Getting Files Out to a Specific Place

If you want the files to come out in a particular folder, just use the -d (directory) option and tell it where you want the archive to be unpacked.

unzip -q source_code.zip -d ./development

Unlock ZIP Files with Passwords

When a ZIP file is locked with a password, unzip will request the password from you. If you don't enter the correct password, unzip won't be able to open and extract the files inside.

unzip -q source_code.zip

If you're not worried about others seeing or saving your password, you can simply type it directly into the command line using the -P (password) option. Remember to use a capital "P" for this.

unzip -P fifty.treacle.cutlass -q source_code.zip

Excluding Files

If you want to keep certain files from being extracted, you can use the -x (exclude) option. For instance, if you want to extract all the files except for the ones that end with ".h" in their names, you can do that.

unzip -q source_code.zip -x *.h

Overwriting Files

Imagine you've extracted some files from an archive, but then oops! You accidentally delete a few of them. No worries, a quick fix is to extract those files again. However, if you try to do that in the same folder where you did it before, the extraction tool (unzip) will ask you what to do with the existing files. It's looking for one of these answers:

• "y": Yes, go ahead and replace this file.

• "n": No, don't replace this file.

• "A": Replace all of them, just go for it.

• "N": Don't replace any of them, keep what's there.

• "r": Rename the file, give it a new name when prompted.

If you want to make unzip replace existing files without asking, you can use the "-o" option.

unzip -o -q source_code.zip

To replace the files that are missing, you can make unzip only take out the files from the archive that aren't already in the destination folder. To do this, simply use the -n (never overwrite) option.

unzip -n source_code.zip

Viewing What's in a ZIP File

Sometimes, it's handy and informative to check out the files inside a ZIP file before you actually unzip it. You can achieve this by using the -l (list archive) option. To make the information easier to read, it's displayed one screen at a time using the "less" command.

unzip -l source_code.zip | less

The result displays the folders and files inside the ZIP file, along with their sizes and the date and time they were included in the archive. To exit this view, simply press the "q" key.

You can also take a look inside a ZIP file using different methods to find various kinds of information, as we'll discover.

Putting a Password on Your ZIP with the zipcloak Tool

If you've made a ZIP file but forgot to lock it with a password, don't worry. You can easily set a password for the ZIP file using the zipcloak tool. Just mention the ZIP file's name when you run the command. It will ask you to pick a password, and you'll need to type it in again to confirm.

zipcloak source_code.zip

Checking File Info Using the zipdetails Tool

The zipdetails command provides a ton of info about the ZIP file. To manage all that information without getting overwhelmed, it's a good idea to use "less" to view it bit by bit.

zipdetails source_code.zip | less

Keep in mind that even when you set a password for your ZIP file, the file names inside the ZIP can still be seen. This information is stored separately inside the ZIP and isn't mixed up with the encrypted data.

Find Text Inside the File with the zipgrep Tool

You can use the zipgrep tool to look for specific words or text inside files within a ZIP file. In this example, we're trying to find out which files inside the ZIP file contain the text "keyval.h."

zipgrep keyval.h source_code.zip

You can notice that the files slang.c and getval.c have the text "keyval.h" in them. Additionally, there are two identical copies of these files in separate folders within the ZIP file.

Checking Details with the zipinfo Command

The zipinfo command is another way to peek inside a ZIP file. Just like before, we use "less" to make the output easier to read.

zipinfo source_code.zip | less

• The output you see has several pieces of information from left to right:

• File permissions (like who can read or write the file)

• The version of the tool used to make the ZIP file

• The original file size

• A file descriptor (explained below)

• How the file is compressed (usually "deflation" here)

• The date and time the file was created or modified

• The name of the file and any folders it's in

The file descriptor has two parts. The first part can be a "t" (for text) or a "b" (for binary), showing what kind of file it is. If it's a capital letter, that means the file is encrypted. The second part can be one of four characters, telling you what kind of extra information is there for this file:

• A hyphen (-) if there's no extra info

• "l" if there's an extended local header but no extra field

• "x" if there's no extended local header but there's an extra field

• "X" if there's an extended local header and an extra field

Dividing the File Using the zipsplit Tool

If you want to share a ZIP file with someone, but there are issues like size limits or file transfer problems, you can use the zipsplit tool to divide the big ZIP file into smaller ones.

With the -n (size) option, you can choose how big each new ZIP file should be. For instance, if we have a file called source_code.zip, and we don't want any of the new ZIP files to be larger than 100 KB (102400 bytes), we can do that.

zipsplit -n 102400 source_code.zip

The size you pick should not be smaller than any of the sizes of the files in the ZIP file.

Post a Comment

Previous Post Next Post