How to zip a folder in Ubuntu Linux / Debian Linux
Tags: ubuntu, linux, beginners, tutorial
Published: 2022-06-26 20:20:14

Zip is the most widely used archive file format that supports lossless data compression.

A Zip file is a data container containing one or more compressed files or directories. Compressed (zipped) files take up less disk space and can be transferred from one to another machine more quickly than uncompressed files. Zip files can be easily extracted in Windows, macOS, and Linux using the utilities available for all operating systems.

Unzip is a utility that is not available on most Linux flavors by default, but can be easily installed. By creating .zip files you can match .tar.gz file compression!

This quick blog shows you how to Zip (compress) files and directories in Linux using the zip command.

## What Is Zip Used For?
Below are a few scenarios in which you may choose to use zip files:
- When you are frequently working between Windows and Unix-based systems. Not only does this compress files but also is a file package utility. Works on multiple operating systems
- To save bandwidth. If you have limited or restricted bandwidth, then zip can be used between two servers for file transfer
- Transfers files quickly. Zip utility reduces file size, therefore reducing transfer time
- Upload or download directories at a faster speed
- Save disk space
- Unzip password-protected .zip files
- Enjoy a good compression ratio
Remember, before taking advantage of Unzip on Linux, you’ll have to SSH into your virtual private server.

## Install Zip on Debian and Ubuntu Systems
Installing unzip is easy! With Ubuntu and Debian use the command below to install unzip:
```
sudo apt install unzip
```
Sit back and wait a minute, until the installation is finished.

To create zip files, you’ll also have to install zip. You can do this with the following command:
```
sudo apt-get install zip
```

## Install Unzip on Linux CentOS and Fedora
This again is simple and can be done using the below command:
```
sudo yum install unzip
```
Once the installation is complete you can check the path with the following command:
```
which unzip
```
After you execute the direction in the command line, you should get an output that looks like this:
```
/usr/bin/unzip
```
You can also confirm everything is installed properly by using the command below. It will give a verbose with unzip utility details.
```
unzip -v
```

## How to Use Zip and Unzip in Linux
Now that we know how to install the utility, we can start learning its basic uses of it:

**zip Command**
Zip is a command-line utility that helps you create Zip archives.

The zip command takes the following syntax form:
```
zip OPTIONS ARCHIVE_NAME FILES
```
To create a Zip archive in a specific directory, the user needs to have write permissions on that directory.

Zip files do not support Linux-style ownership information. The extracted files are owned by the user that runs the command. To preserve the file ownership and permissions, use the tar command.

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

## Create Zip Files in Linux
The basic syntax to create a .zip file is:
```
zip options zipfile list_Of_files
```
o test this, we created two files – ExampleFile.txt and ExampleFile1.txt. We’ll compress them into sampleZipFile.zip with the following command:
```
zip sampleZipFile.zip ExampleFile.txt ExampleFile1.txt
```

## Using Linux to Unzip a file
The unzip command can be used without any options. This will unzip all files to the current directory. One such example is as shown below:
```
unzip sampleZipFile.zip
```
This by default will be unzipped in the current folder provided you have read-write access.

## Remove a File from a .zip File
Once a .zip file is created, you can remove or delete files in it. So, if you want to remove ExampleFile.txt from the existing sampleZipFile.zip, then you can use the following command:
```
zip –d sampleZipFile.zip ExampleFile.txt
```
Once this command is executed, you can unzip the .zip file using:
```
unzip sampleZipFile.zip
```
Over here you will find that ExampleFile.txt has been removed and can’t be seen on extraction.

## How to Update Zip Files
Once a .zip file is created, you can add a new file to an existing .zip file. Suppose a new file ExampleFile2.txt needs to be added to the already existing sampleZipFile.zip. You can do this with the command shown below:
```
zip –u sampleZipFile.zip ExampleFile2.txt
```
Now if you extract sampleZipFile.zip, you will find the new file ExampleFile2.txt added to it.

## Move a File to a Zip
You can easily move specific files to a zip file. That means that after adding the files, they will be deleted from their original directories. This is mostly used when you have a large file or directory, but need to conserve disk space. This is done by adding the -m option. A sample of this command would be:
```
zip –m sampleZipFile.zip ExampleFile2.txt
```

## Recursive Use of Zip on Linux
The -r option is used to recursively zip files. This option will compress all the files present within a folder. An example of such a command is shown below:
```
zip –r sampleZipFile.zip MyDirectory
```
In the example, MyDirectory is a directory that has multiple files and sub-directories to be zipped.

## Exclude Files in a Zip
While creating a .zip file, you can exclude unwanted files. This is done by using the -x option. Below is an example:
```
zip -x sampleZipFile.zip ExampleFile.txt
```
Here ExampleFile.txt will not be added to the sampleZipFile.zip.

## Unzip to a Different Directory
In case you do not want to unzip to the current directory but want to specify a directory location, then this can also be done. Use the -d option to provide a directory path in the unzip command. An example of such a command is shown below:
```
unzip sampleZipFile.zip -d /usr/sampleZip/ExampleDir
```

## Use Linux Unzip with Multiple Zip Files
If you want to unzip multiple zip files existing within your current working directory then you can use a command as shown below:
```
unzip '*.zip'
```
This command will unzip all the individual zip files.

## Creating 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
```

## 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), or t (terabytes).
```
zip -s 1g -r archivename.zip directory_nameCopy
```
The command above will keep creating new archives in a set after it reaches the specified size limit.
```
archivename.zip
archivename.z01
archivename.z02
archivename.z03
archivename.z04
```