How To Zip Folder And Subfolders In Linux

In Linux, one very useful feature is the ability to compress files and folders into a single .zip file. This can come in handy when you want to transfer multiple files or back up your data. In this post, we’ll walk through the steps to zip folder and subfolders in Linux.

Prerequisite

Before we start, you need to ensure that zip is installed on your system. You can verify this by running the following command in your terminal:

zip -v

If zip is not installed, you would have to install it. Here is how to install zip on Ubuntu:

sudo apt-get install zip

Zipping a Folder and Subfolders

To zip a folder and all its subfolders, we can use the following command:

zip -r output_file.zip folder_to_be_zipped

The -r option tells zip to recursively zip the directory, which is what allows it to include subdirectories. The folder_to_be_zipped is the folder that you want to zip, and the output_file.zip is the name of the zip file that will be created.

Excluding Files and Directories

In some cases, you might want to exclude certain files or directories when creating your zip file. You can do this by adding the -x option followed by the pattern to exclude:

zip -r output_file.zip folder_to_be_zipped -x \*.git\* \*node_modules\* \*.DS_Store\*

The above command will zip the folder and subfolders but will exclude any file or directory that matches the patterns provided after the -x option.

Conclusion

This is a basic introduction to zipping folders and subfolders in Linux. With these commands, you can easily compress your data and make it easier to transport. It’s worth noting that Linux offers many more features and options for zipping files, so don’t hesitate to check out the man page for zip for more information:

man zip