How To Zip A File In Linux Command Line

If you are a Linux user, there may be times when you need to compress files or folders to save storage space, for secure and efficient data transfer, or for the purpose of backing up data. In this tutorial, we will cover a basic yet essential command line operation: how to zip a file or a folder using Linux command line.

Preparation

Before we start, ensure you have the ‘zip’ command installed on your Linux system. You can check this by simply typing ‘zip’ in your terminal. If it’s not already installed, you can do so using the following command:


sudo apt-get install zip

Zipping a File

The basic syntax to create a zip file is as follows:


zip [options] zipfile_name file1 file2

Let’s say you have a file named ‘example.txt’ in your current directory and you want to compress it. To do this, type the following command in your terminal:


zip example.zip example.txt

After executing this command, you will see that a file named ‘example.zip’ has been created in the same directory. This is the zipped version of ‘example.txt’.

Zipping a Folder

To zip a directory, you’ll need to add the -r option, which stands for “recursive”. For example, to zip a directory named ‘test_folder’, use the following command:


zip -r test_folder.zip test_folder

This will create a zip file named ‘test_folder.zip’ which contains all files and subdirectories inside ‘test_folder’.

Conclusion

With the ability to zip files and folders directly from the command line, you can perform these common tasks in seconds, without ever needing to leave your terminal. This is just another example of how powerful and flexible Linux can be for users who are willing to dive into its command-line interface.