How to Exclude Files from a Zip Archive Linux Mint

You can easily exclude specific files and/or folders from a zip archive with Linux Mint and it's terminal.

In this post we will demonstrate several useful examples on how to zip folder and exclude specific files and folders. Excluding files might be due to sensitive data, big size or specific file type like: zip, png or csv.

Step 1: How to zip files with terminal in Linux Mint

To begin, lets show how folder or file can be archived with terminal in Linux Mint.

In the example below, we'll work with a folder called: Mapache which will be used for our example. So you can download the folder from the link above.

So to zip the folder Mapache we can use next commands:

cd ~/Downloads
zip -r  mapache.zip Mapache

Note: Command cd ~/Downloads is optional and navigates to the parent folder of Mapache.

Zip file with keeping date and time

If you like to keep history of the dates of the archives than you can try with the next command:

zip -r  mapache_$(date +\%Y-\%m-\%d-\%H-\%M).zip Mapache

which will produce a zip file like:

mapache_2021-07-21-00-26

Zip folder without including parent directory

To zip a folder without including the parent folder use:

cd Mapache
zip -r  ../mapache_$(date +\%Y-\%m-\%d-\%H-\%M).zip . -x */\.* *.git* *.idea* "**/screenshot.png" ./screenshot.png

Step 2: How to exclude files from a zip archive

To exclude a file from archive using the zip command you can use:

  • -x
  • --exclude

both are interchangeable.

Exclude file from the root folder

So for example let's try to exclude a file: screenshot.png. If the file is at the top level than it can be excluded by

zip -r  mapache.zip Mapache -x "screenshot.png"

for excluding multiple files we can list than all after the -x:

zip -r  mapache.zip Mapache -x "screenshot.png" "donate.gif"

Note 1: this will work only if the files are at the top level.

Note 2: You can have multiple exclude options: zip -r mapache.zip Mapache -x "screenshot.png" -x "donate.gif"

Exclude file from the subfolders

If the files are located in a subfolder than they can be excluded by slightly different syntax:

zip -r  mapache.zip Mapache  -x "**/screenshot.png"

where **/ is used to show recursive search.

Exclude files by file extension

Let say that you would like to** exclude specific file types like: .csv**, .zip etc. This can be done by next command:

zip -r  mapache.zip Mapache  -x "*.csv"

Step 3: How to exclude folders from a zip archive

To exclude folders from a zip archive you can follow a bit different syntax. Let's say that you would like to exclude folder documentation then you can do:

zip -r  mapache.zip Mapache --exclude=*documentation*

Step 4: Popular file/folder exclusions - exclude git, hidden files

In this step are listed the most popular files exclusions like:

To exclude all folders (and files) starting with a dot(hidden files)

zip -r  mapache.zip Mapache --exclude=*/.*

To exclude .git directory from a zip Archive

zip -r  mapache.zip Mapache -x "*.git*"

or

zip -r  mapache.zip Mapache -x '*.git*'

To exclude .git directory from a zip Archive with git

As an alternative of zip command for git project you can use:

git archive -o mapache.zip HEAD

The plus of this command is that this will exclude the git folder and also anything that is in the gitignore file.

To exclude zip files from a zip Archive

The next command will prevent all archive files from zipping.

zip -r  mapache.zip Mapache -x \*.zip

Step 5: Script to zip files and exclude specific files and folder

Let's say that you want to build a script which is going to zip the folder and exclude folders and files.

To do so you can create new text file called mapache.sh and put next content inside:

#!/bin/bash
cd Mapache
zip -r  ../mapache_$(date +\%Y-\%m-\%d-\%H-\%M).zip . -x */\.* *.git* *.idea* "**/screenshot.png" ./screenshot.png

Make it executable by:

chmod +x mapache.sh

The file should be located at the same level as folder Mapache.