Linux logo

Why using zip in Linux?

ZIP is a compression and file packaging utility for Unix. Each file is stored in single .zip {.zip-archivename} file with the extension .zip.

The zip is used to compress the files to reduce file size and also used as a file package utility. zip is available in many operating systems like Unix, Linux, Windows OS, etc.

Not only it compresses the files but bring many files into a single file that may help you sending email as a single file or there may be other uses.

To zip all files in a given directory on Linux with the zip tool, you can use * with the zip command. This will take care of files with and without extensions since extensions are not so special on Linux. The * will match zero or more characters, with the dot included. The files that begin with a dot however are not considered when using * (or even *.*).

General syntax of using the zip command

Following is the general syntax of zip command:

$ zip OPTIONS Zip_File_Name FILES

An example of creating a zip file

To create a .zip (packaged and compressed) file from the command line, you can run a similar command like the one below, The -r flag enables recursive reading of files directory structure (explained more in coming section).

$ zip -r tecmint_files.zip tecmint_files

How to create a password protected zip file?

If you have sensitive information that needs to be stored in the archive you can encrypt it using the -e option:

zip -e  archivename.zip directory_name

You will be prompted to enter and verify the archive password:

Enter password:

Verify password:

What if zip utility is not installed?

The zip utility is not installed by default in most Linux distributions, but you can easily install it using the package manager of your distribution.

How to install Zip in Debian and Ubuntu

$ sudo apt install zip

 

Install Zip on CentOS and Fedora

sudo yum install zip

 

Using the -r option

To zip a directory recursively, use the -r option with the zip command and it will recursively zip the files in a directory. This option helps you to zip all the files present in the specified directory.

$zip –r filename.zip directory_name

Where directory_name is the folder which contains your files. The zip command in Linux thus formed will have the same directory structure along with the files. Note that when extracting the zip file, it will save all extracted files in one subdirectory.

Zip multiple files example

To zip one or more files, specify the files you want to add to the archive separated by space as shown below:

$ zip archivename.zip file1 file2 file3

 

By default the zip command prints the names of the files added to the archive and the compression method.

When specifying the Zip archive name if you omit the .zip extension it will be added automatically unless the archive name contains a dot. zip archivename.zip filename will create an archive with the same name as would zip archivename filename.

Creating Split Zip File

Imagine you want to store the Zip archive on a file hosting service that has a file size upload limit of 1GB and your Zip archive is 5GB.

You can create a new split Zip file using the -s option followed by a specified size. The multiplier can be

  • k (kilobytes)
  • m (megabytes)
  • g (gigabytes)
  • t (terabytes)

A command example:

zip -s 1g -r archivename.zip directory_name

 

How to unzip a zipped file?

Let us say we have a zip file name test_files.zip, you can run the unzip command as follows.

$ unzip test_files.zip

 

Extract Zip File to Specific or Different Directory

To extract/unzip .zip archive files to specific or different directory from the command line, include the -d unzip command option as shown below. We will use the same example above to demonstrate this.

This will extract the .zip file content into the /tmp directory:

$ mkdir -p /tmp/unziped

$ unzip test_files.zip -d /tmp/unziped

$ ls -l /tmp/unziped/

 

An example of -x option

The –x option is used to exclude the files in creating the zip. Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

$zip –x filename.zip excluded_file

 

For example:

$zip –x myfile.zip hello.txt

All files will be compressed except for the hello.txt