Do you want to send files but you are facing a problem due to their large size? It has the solution. For this purpose, we have a tool known as zip, which is used to compress all the files and store them in a single folder. Now you can send the compressed zip folder containing all the files easily to anyone. But there is a folder having 100 files and you have to forward this folder excluding some specific files. Now instead of going to the folder and searching the specific files, we can exclude them by using some commands.
In this article, we will discuss how to make a zip folder by excluding specific files and how it works.
Syntax
Generally, all the Linux distributions have default zip packages installed. The syntax of the command excluding files and directories is as follows:
1 | zip -r [filename].zip [directory_name_to_zip] -x [file_to_exclude] |
In the above syntax, the “-r” flag is used to append files, and the “-x” flag is used to exclude files. Now to understand it more clearly let us consider an example.
Examples to Excludes Files in Zip Archive
I have created a sample directory structure including some files. Where a directory named “docs” contains one directory and some files as shown below screenshot:

Let’s consider the above structure, here are a few examples to learn about excluding files from the zip archive.
01. Exclude a directory
Create an archive file excluding the cache directory. Use the following command to create a new archive named “docs.zip” excluding the “cache” directory. So in command, it will be written as:
zip -r docs.zip docs -x "docs/cache/*"

02. Exclude a single file
Define the full file path to exclude a single file from the archive file with the zip command. For example, to exclude index.html from the docs directory, type:
zip -r docs.zip docs -x "docs/index.html"

03. Exclude files with wildcard
You can also use wild card characters to exclude multiple files from the zip archive. For example, to exclude all files with the “.log” extension, run:
zip -r docs.zip docs -x "*.log"

04. Exclude multiple files
You can define -x
multiple times in a single zip command to exclude multiple files and directories from the zip archive.
zip -r docs.zip docs -x "/docs/README.md" -x "docs/cache/*"

05. Exclude “.git” directory
The source code managed with Git contains a “.git” directory under the root directory. Sometimes you may not be required to archive this directory. Use the following command to exclude .git
directory and its contents:
zip -r filename.zip my_dir -x "*.git*"
06. Exclude “.svn” directory
Applications source code managed through SVN contains the “.svn” directory. Use the following command to exclude the .svn directory from the zip archive.
zip -r filename.zip my_dir -x "*.svn*"
07. Excluding “node_modules” Directory from Zip Archive
All the Node.js modules are installed under the node_modules directory. While archiving the source code, you can ignore the “node_modules” directory with the below-mentioned command:
zip -r mydir.zip myDir -x "node_modules*"
Conclusion
With the advancement of technology, it’s not a big issue to resolve anything. Now we can easily make a single zip file of many files and can email it to anyone easily. In this article, we learned how we can make zip files excluding some files that we don’t want to be a part of the zip file
2 Comments
Thank you for the article, it was really helpful!
Hi. You can use a free online tool to create ZIP https://freetools.site/file-compressor/zip