How to zip a file in Linux? Use different compression techniques right from the Linux Terminal


Sending multiple files in one go is something we all do very often, no matter whether we are using a PC or a smartphone. While you can easily use a zipping tool to zip and unzip files on Windows, the process is a little different, if not complicated, on a computer running a Linux distro. 

So, today on InkedFreedom, I will talk about how to zip a file in Linux. You can use both File Explorer on your Linux system, as well as the Terminal to zip a file in Linux. Not just at the time of sending files, zipping files can be a tool to compress disk space, and have several other advantages. 

So, without any further ado, let's get started with how to zip a file in Linux using the Terminal. By using the Terminal, you can go pro, and archive files just the way you want them.

Downloading the ‘zip’ utility

The zip utility is pre-installed on most Linux distributions. However, if it is not installed, you can use the following commands to download the utility on your Linux distro. Just type in ‘zip’, and hit the enter key. If Terminal returns some error, you have to download the utility.

sudo apt install zip *On Debian-based distros and Ubuntu.

sudo yum install zip *On CentOS and Fedora-based distros.

While you execute zip commands, make sure you USE ‘zip’. DO NOT USE ‘Zip’ or ‘ZIP’.

Zip a set of files in a directory

If you want to zip a file in Linux, or want to zip multiple files in Linux, here’s the command format.

zip <path/archive_name> <path/file_name1> <path/file_name2> <path/file_nameN>

Here’s an example.

zip testfile1.zip \test/Untited1.odt \test/Untitled2.odt

This command will zip the files with the name Untitled1.odt and Untitled2.odt within the directory ‘test’, in the user’s home directory with the filename testfile1.zip, in the current directory.

Zip a whole directory

If you want to zip all the files within a specified directory, you can zip the whole directory instead. Here’s the command format.

zip -r <path/file_name> <path/directory>

Here’s an example.

zip -r testdir.zip test

This will zip the directory with the name ‘test’ in the current directory along with all its contents in a file with the name testdir.zip, in the current directory, as well.

The ‘-r’ flag is used for the recursion algorithm. This is used to recursively put all the files of the directory within the archive.

Zip a file or directory with encryption (set a password to open the archive)

If you are sharing files or the archive and you don’t want unintended users to access the archive, you can also encrypt the archive for security. Here’s the command format to zip a file in Linux and protect it with a password.

zip -e <path/file_name> <path/directory>

Optionally you can also zip a whole directory with its contents, and then encrypt it with a password by adding the flag ‘-r’ to recursively add all the files to the archive within the specified directory.

Here’s an example.

zip -e -r testencrypt.zip test

After you hit the enter key, you will have to type in the password two times, and hit the enter key. Just like the art of entering a password on Linux, the password will not be displayed while you type.

This command will archive all the content within the directory test, in a file with the name testencypt.zip, both within the current directory, and will then encrypt the file with a password of your choice.

Zip a file or directory fast or use the best level of compression

If you are in a hurry and want to zip multiple files or directories fast, you can use ‘zip -1’. If you just want to archive the files without actually compressing them, use ‘zip -0’. However, if you want to get the best level of compression, while you zip a file in Linux, use ‘zip -9’. All these commands should be followed by the path of the archive and the file to be archived.

Here are some examples for your reference.

zip -1 testfile.zip test.txt *This will compress the file fast.

zip -0 testfile.zip test.txt *This will just add the file to the archive without compressing it.

zip -9 testfile.zip test.txt *This will create the archive with the best level of compression. The process will take a significant amount of time.

As you can find above, the ‘zip’ command has several options. Just use the flags based on the situation and the way you want to create an archive. You can combine the flags to create an archive exactly the way you want.

Say, for example, you want to archive a folder named ‘test’, along with all the contents to a file named test.zip, encrypt it, and use the best level of compression. For this situation, the command will go as follows.

zip -r -9 -e test.zip test

The directory named ‘test’ should be in the current directory, and the ‘test.zip’ will also be created in the current directory. 

If you don’t specify the compression level, the default compression level will be ‘6’ on a scale that ranges from 0 to 9. 0 refers to no compression, and 9 refers to the best level of compression.

Just type in ‘zip’ and hit the enter key to find out other ways to zip a file. I just mentioned the most basic options most people will probably use to zip a file in Linux, or zip multiple files in Linux.

So, that was all about how to zip a file in Linux? Do you have any questions? Feel free to comment on the same below.


Comments