How to zip or unzip files from the Linux terminal

Contents

Linux shell indicator

ZIP files are a universal file that is commonly used on Windows systems, macOS and Linux included. You can create a zip file or unzip files from one with some common Linux terminal commands.

The ZIP compressed file format

Thanks to the dominance of the ZIP format in the Windows environment, ZIP files are probably the most common form of compressed archive in the world.

While the .tar.gz and tar.bz2 files are common on Linux, Windows users will probably send you a ZIP file. AND, if you want to archive some files and send them to a Windows user, ZIP format will be the easiest and most compatible solution for everyone.

RELATED: How to extract files from a .tar.gz or .tar.bz2 file on Linux

zip, unzip and other utilities

You may already know that Linux and Unix-like operating systems, like macOS, they have tools that allow you to create ZIP archives and extract archives from them, called zip and unzip. But there is a whole family of related utilities, What zipcloak, zipdetails, zipsplit , and zipinfo.

We checked some Linux distributions to see if they included these utilities in the standard installation. All the utilities were present in Ubuntu 19.04, 18.10 and 18.04. They were also present in Manjaro 18.04. Fedora 29 included zip and unzip, but none of the other utilities and that was also the case for CentOS.

To install the missing items in Fedora 29, use the following command:

sudo dnf install perl-IO-Compress

zip installation command for fedora

To install the missing items in CentOS 7, use the following command:

sudo yum install perl-IO-Compress

Zip installation command in Centos

If any of the zip utilities are missing on a Linux distribution not mentioned above, use the package management tool of that Linux distribution to install the required package.

How to create a ZIP file with the zip command

To create a ZIP file, must indicate zip the name of the archive file and what files to include in it. You don't need to add the extension “.zip” You don't need to add the extension, but it doesn't hurt if you do.

To create a file called source_code.zip which contains all C source files and header files in current directory, I would use this command:

zip source_code *.c *.h

Each file is listed as it is added. The name of the file and the amount of compression that was achieved on that file is displayed.

If you look at the new ZIP file, You don't need to add the extension “.zip” You don't need to add the extension zip.

ls -l source_code.zip

If you don't want to see the output of zip as the ZIP file is created, Use the -q Option (silent).

zip -q source_code *.c *.h

Include directories in ZIP files

To include subdirectories in the ZIP file, Use the -r (recursive) and include the subdirectory name in the command line. To create a ZIP file as before and also include the file's subdirectory, use this command.

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

To be considerate of the person who will extract the files from the ZIP archive you are creating, it is often good manners to create ZIP archives with the files within a directory. When the person receiving the ZIP file extracts it, all files are neatly placed within a directory on your computer.

In the following command, we are going to archive the work directory and all subdirectories. Note that this command is issued from the parent directory of the work file.

zip -r -q source_code work/

Compression level setting

You can set how much compression is applied to files as they are added to the ZIP archive. The range is 0 a 9, being 0 without any compression. The higher the compression, more time will take to create ZIP file. For ZIP files of modest size, the time difference is not a significant penalty. But then, for ZIP files of modest size, the default compression (level 6) it's probably good enough anyway.

To get zip to use a specific level of compression, pass the number as an option on the command line, with a “-“, So:

zip -0 -r -q source_code work/

The default compression level is 6. You do not need to provide the -6 option, but it won't do any harm if you do.

zip -r -q source_code work/

The maximum level of compression is the level 9.

zip -9 -r -q source_code work/

With the selection of files and directories that are archived here, the difference between no compression (level 0) and the default compression (level 6) it 400K. The difference between the default compression and the highest compression level (level 9) it's only 4K.

It might not seem like much, but for files containing hundreds or even thousands of files, the small amount of additional compression per file would add up to a worthwhile space saver.

Add passwords to ZIP files

Adding passwords to ZIP files is easy. Use the -e (encrypt) and you will be asked to enter your password and re-enter it for verification.

zip -e -r -q source_code work/

How to unzip a ZIP file with the unzip command

To extract the files from a ZIP archive, use the unzip command and provide the ZIP file name. Please note that you do You don't need to add the extension “.zip”.

unzip source_code.zip

As the files are extracted, are listed in the terminal window.

ZIP files do not contain details about file ownership. All files that are checked out have the owner set to the user who checked out them.

Like zip, unzip have a -q (silent), so you don't need to see the list of files as they are extracted.

unzip -q source_code.zip

Extract files to a destination directory

To extract the files to a specific directory, use el -d (directory) and provide the path to the directory where you want the file to be extracted.

unzip -q source_code.zip -d ./development

Extract password protected ZIP files

If a ZIP file has been created with a password, unzip it will ask for the password. If you don't provide the correct password, unzip will not extract the files.

unzip -q source_code.zip

If you don't mind other people seeing your password, nor is it stored in your command history, you can supply the password on the command line with the -P (password) option. (You don't need to add the extension “P” capital letter)

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

Exclude files

If you don't want to extract a particular file or group of files, Use the -x (delete) option. In this example, You don't need to add the extension “.h”.

unzip -q source_code.zip -x *.h

File overwriting

Suppose you have checked out a file, but you have deleted some of the mistakenly extracted files.

A quick solution for that would be to extract the files one more time. But if you try to extract the ZIP file in the same directory as before, unzip will ask you for a decision on overwriting the files. You will expect one of the following responses.

Apart from r (rename) answer, these answers are case sensitive.

  • and: Yes, overwrite this file
  • North: No, don't overwrite this file
  • A: Everything, overwrite all files
  • NORTH: None, does not overwrite any of the files
  • r: Change the name, extract this file but give it a new name. You will be prompted for a new name.

To force unzip to overwrite any existing file use the -o (overwrite) option.

unzip -o -q source_code.zip

The most efficient way to replace the missing files would be to have unzip Extract only files from the archive that are no in the destination directory. To do this, use el -n (never overwrite) option.

unzip -n source_code.zip

Look inside a ZIP file

It is often helpful and instructive to view a list of the files within a ZIP file before extracting it.. You can do this with the -l (file list) option. It is channeled through less so that the output is manageable.

unzip -l source_code.zip | less

The output shows the directories and files within the ZIP file, their length and the date and time they were added to the file. Press “q” now. less.

There are other ways of looking inside a ZIP file that give different types of information, how will we see.

Add a password with the zipcloak command

If you have created a ZIP file but forgot to add a password, What can you do? You can quickly add a password to the ZIP file using the zipcloak command. Pass the name of the ZIP file in the command line. You will be asked for a password. You must verify the password by entering it a second time.

zipcloak source_code.zip

View file details with the zipdetails command

the zipdetails The command will show you a batch information about the ZIP file. The only sensible way to handle the amount of output this command can give is to pipe it less .

zipdetails source_code.zip | less

Please note that the information will include file names even if the ZIP file is password protected. This type of information is stored within the ZIP file as metadata and is not part of the encrypted data..

Search within the archive with the zipgrep command

the zipgrep The command allows you to search inside the files in a ZIP file. In the following example, You don't need to add the extension “You don't need to add the extension” You don't need to add the extension.

zipgrep keyval.h source_code.zip

We can see that the files slang.c and getval.c contain the string "keyval.h". We can also see that there are two copies of each of these files in different directories of the ZIP file.

View information with the zipinfo command

the zipinfo The command gives you another way to look inside a ZIP file. As before, we funnel the output through less.

zipinfo source_code.zip | less

From left to right, the output shows:

  • File permissions
  • The version of the tool used to create the ZIP file
  • The size of the original file
  • A file descriptor (Described below)
  • The compression method (deflation, in this case)
  • The timestamp and data
  • The name of the file and any directory

The file descriptor consists of two characters. You don't need to add the extension “t” or one “b” You don't need to add the extension. If it is a capital letter, the file is encrypted. The second character can be one of four characters. This character represents what type of metadata is included for this file: none, an extended local header, a “You don't need to add the extension” the both.

  • -: If none exists, the character will be a dash.
  • l: if there is an extended local header but no extra field
  • X: if there is no extended local header but there is an extra field
  • X: if there is an extended local header and there is an extra field

Split the file with the zipsplit command

If you need to send the ZIP file to someone else but there are size restrictions or problems with transmitting the file, you can use the zipsplit command to split the original ZIP file into a set of smaller ZIP files.

the -n The option (size) allows you to set a maximum size for each of the new ZIP files. In this example, we are dividing the source_code.zip proceedings. We do not want any of the new ZIP files to exceed the 100 KB (102400 bytes).

zipsplit -n 102400 source_code.zip

The size you choose cannot be smaller than the size of any of the files in the ZIP file.

With these commands, you can create your own ZIP files, unzip the ZIP files you receive and perform other operations on them without leaving the Linux terminal.

setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.