How Do I Zip All Files In A Directory In Linux?

The zip is a utility to compress files and directories with no data loss. It is also used as a file package utility, and all distributions of Linux support it. The zip file is useful when you have low bandwidth for sending data between 2 servers; the user can zip the file and send it. It consumes less storage which will make them easy to store and transfer.

This article will demonstrate the methods to zip all files in a directory. The content for the post is as follows:

Installation of zip

In most Linux distributions, it comes with pre-installation, but users can also install it manually. Use the following command depending on the distribution:

For Debian/Ubuntu:

$ sudo apt install zip

For CentOS/RHEL:

$ sudo yum install zip

To verify whether the zip utility is installed or not, run the zip command with “–version”:

$ zip --version

Zip version 3.0 is installed in the operating system.

The syntax for the zip command is given below:

Syntax

$ zip [File_Name.zip] [Source_File/Directory]

Type the “zip” keyword, the file name with .zip extension, and then the source file/directory name.

How do zip Files in Linux?

For a better understanding of zipping any files in Linux, check out the examples below:

Example 1: Zipping a File

We have a “date.txt” file in our home directory. Let’s zip it using the zip utility. Run the given command in the terminal:

$ zip date.zip  date.txt

The date.txt file is zipped with the name date.zip. Furthermore, the zip command has also displayed the percentage of compression.

Example 2: Zipping Multiple Files

To zip the multiple files, type the file using the zip utility as follows:

$ zip All_Files.zip  file1.txt file2.txt file3.txt

All the files have been compressed and stored in the “All_Files.zip”.

Method 1: Zip All Files in a Directory Through CLI

For compressing all the files in the directory, check out the implementation of the example below.

Example 1: Compressing the Directory Including All files

To compress all files present in the directory, use the “r” flag, which will compress the entire directory, including its files:

$ sudo zip -r Files.zip /Henry

The directory will be compressed including all files.

Let’s check the created zip file for the directory.

$ ls

The zip file with the name “Files.zip” has been created.

Example 2: Compressing the Directory Including All files to Another Directory

To compress the entire directory to the specified directory you can specify the path before the zip file name. Run the given command in the terminal:

$ sudo zip -r /home/itslinuxfoss/Desktop/Files.zip Henry

The directory has been compressed to the “Desktop” directory.

Let’s verify it using the “ls” command:

$ ls Desktop

The zip file is present in the “Desktop” directory.

Method 2: Zip All Files in a Directory Through GUI

For zipping all files in Directory, the GUI method is also available. Just right-click on the specific directory which you want to compress and click on the “compress” option from the drop-down:

Once you click on the compress option it will ask you to enter the name of the zip file and choose the “.zip” extension from the given drop-down. After that, click on the create button as shown in the below image:

The zip file will be created which can be seen in the following screenshot:

That’s how all files in a directory are zipped in Linux.

Conclusion

In Linux, zip is a utility to compress files or directories without changing their data. To zip all the files in a directory user can use the “r” flag in the command or zip the directory manually by right-clicking and hitting the “compress” option. This write-up has briefly illustrated the method to zip files and directories using the zip utility.