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.
How to exclude files from a zip archive
Generally all the Linux distribution have default zip package installed. The syntax of the command with excluding file and directories is as follows:
zip -r [filename].zip [directory name to zip] -x [file/directory name/path to exclude]
In the above syntax, the “r” flag is used to append files and “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 in below screenshot:

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

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

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

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

Real World Examples
Here is some read world examples, which is frequently used by the users.
01. Excluding “.git” Directory from Zip Archive
The source code managed with Git contains a “.git” directory under root directory. Sometimes you may not required to archive this directory. Use the following command to exclude .git
directory and it’s contents:
zip -r mydir.zip myDir -x "*.git*"
02. Excluding “.svn” Directory from Zip Archive
Applications source code managed through SVN contains “.svn” directory. Use the following command to exclude the .svn directory from zip archive.
zip -r mydir.zip myDir -x "*.svn*"
03. Excluding “node_modules” Directory from Zip Archive
All the Node.js modules are installed under node_modules directory. While archiving the source code, you can ignore the node_modules directory with 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
1 Comment
Hi. You can use a free online tool to create ZIP https://freetools.site/file-compressor/zip