How to Zip Files and Directories in Linux?

To share a file containing multiple heavy files, we can create a zip for lossless data transmission. Zip files are useful for sending tens of files in one attachment only. The archive file format “zip” is supported by all the renowned Linux distributions. In this guide, we will use different methods to create and use the zip file.

The article agenda is as follows:

Let’s begin!

Method 1: How to Zip Files and Directories in Linux Using Terminal?

To create a zip file using the zip command, we need the “ZIP” package. If your distribution does not have it. To install the Zip package from the default repository, we can execute the below commands in the different Linux-based distributions:

For Ubuntu:

$ sudo apt install zip

For Arch Linux:

$ sudo pacman -S zip

For Centos:

$ sudo dnf install zip

General Syntax of Zip Command

Before getting into example, let’s have a look at the “zip” utility that will be used to zip files and directories:

$ zip [options] <ZipFile-Name> <file....>

Options Available for Zip Command

There are various options in Zip command which can be checked using the “zip –help” command, but the commonly used options are as follows:

-dDelete/Remove Entries from the Zip file
-DDo not add the directory/folder in the Zip file
-rCheck the directories, again and again, to convert file to Zip file until the Zip file is created
-oStores the files into Zip file without any change/compression
-qHide the status of operation
-eEncypt the Zip file
-mMove to the Zip file and deletes the original file
-nTo avoid compression of the files to create aZip file
-uUpdates the Zip file for changed or new files only

Zip a Single File

To create a zip file “ArchiveFiles” having a single file named “file1.txt”, use the below command:

To list the files in a directory, “ls” command is used:

$ zip Archivefiles.zip file1 $ ls

Zip Multiple Files

To create a zip file of multiple files (“file1.txt”, “file2.txt”, “file3.txt”), utilize the below-written command:

$ zip ArchiveFiles file1.txt file2.txt file3.txt

Zip a Single Directory

To create a zip “ArchiveFolder” having a single folder named “MyFolder1”, the below command is used:

$ zip ArchiveFolder MyFolder1

Zip Multiple Folders

To create a “zip” having a folder with a subdirectory or subdirectories, we cannot use the above command “$ zip ArchiveFolder MyFolder1”, as it will zip the main directory only:

$ zip ArchiveFolder MyFolder1

The output shows only the “MyFolder1” is zipped.

To zip a folder or multiple folders recursively (checks the folder repeatedly until all files are Zipped) with its directory or sub-directories, use the “r” flag with the zip command. To zip the folder “ArchiveFolder” with “MyFolder1” sub-directories, use the following command:

$ zip -r ArchiveFolder MyFolder1

The output verifies the zip of sub-directories of the “MyFolder1”.

 Similarly, we can zip multiple folders with their sub-directories using the “r” flag as follows:

$ zip -r ArchiveFolder MyFolder1 MyFolder2 MyFolder3

Zip Same Type of Files

If you want to zip all the files have the same format, you can use the wild card character. Like, the below command will create a zip of all the “mp4” files in the directory:

$ zip archivename *.mp4

The zip command line utility can also be used to create a password-protected file. Let’s create!

Creating a Password-Protected Zip File Using Terminal

Privacy matters for all, and the same in the case of zip files. To protect the private files/directories, we want to set the password using the “e” flag of the zip command. To set a password to file/folder use the “$ zip -e <Archive.zip> <directory>” command. The folder “MyFolder1” can be made password protected folder using the below command:

$ zip -e ArchiveFolder.zip MyFolder1

These were all the commands to zip files and directories in Linux. Let’s get into the GUI method!

Method 2: How to Zip Files and Directories in Linux Using GUI?

We can create a zip file or files/directories using the “GUI” method. Let’s discuss it!

Zip Files Using GUI

To zip a file/files, select the specified files and select the “Compress” option after right-clicking.

A new “Create Archive” box will open with the options to set the “Name” and select the “.zip” file type. Lastly, Click on “Create”:

The below “.zip” file will be created in your specified folder with selected files in it:

Zip Folders Using GUI

Similarly, for creating the zip folders, select the desired folders and choose the “Compress” option:

Choose the folder “Name” & “Type” of the file and press the “Create” button:

The folders will be added to the zip file as shown below:

These are all the possible methods to zip files and directories in Linux.

Bonus Tip: How to Unzip the Files/Directories in Linux?

You can unzip the files/directories utilizing the “unzip <file>.zip” command or utilize the “d” of the zip command to delete a certain file from a zip file like “-d <zipfile-name.zip> <file-name>”.

That’s the end of this guide!

Conclusion

To create a zip file, use the “Command Line” using the “zip” package or Graphical User Interface (GUI) methods. The “zip” package can be used to zip the file/directory, multiple files/directories, and compress directory/directories with all sub-directories. Moreover, a password-protected zip file can also be created. This post has listed all the possible commands and the GUI method to zip files and directories in Linux.