PureDevOps Community

Exclude files from zip archive in linux

How to exclude files and directories when using “zip” archive in Linux:

You can archive any directory excluding a particular file using the “-x” flag with the “zip” utility. The general syntax is mentioned below:

$zip -r [file_name.zip] [files/folder to zip] -x [file path/name to exclude]

Let’s understand it through an example; I have created a directory by the name of “images” that contains 3 image files, one zipped file, and one sub-directory (my folder) as shown in the following image :

Now launch terminal and type:

$zip -r myfile.zip images -x /images/img2.png

In the above command, I am compressing the “images” directory and excluding an image file “img2.jpg” and saving it as “myfile.zip.”

As it can be seen that “myfile.zip” did not include the “img2.jpg” file. You can also exclude file by mentioning the extension of the file; for instance, I want to exclude all “.jpg” files in the “images” directory and archive it:

$zip -r myfile.zip images -x *.jpg

All the “jpg” files have been excluded in the zip file. You can exclude multiple files and directories by mentioning them in the following way:

$zip -r myfile.zip images -x /images/img2.jpg -x


The “img2.jpg” and “all_images.zip” files will not be archived:

Credit: Linuxhint