A shell alias and script for recursively stripping metadata from image files with mat2.
What is metadata?
Please read the article I wrote about metadata to get an idea about metadata and why it matters to remove it.
The problem
mat2 is an excellent program that removes metadata from files. But the problem I encountered while using it is that it sometimes breaks when it encounters file types such as .xml. So it makes recursive metadata removing problematic.
Solutions
There are two ways to do this.
Alias
You can add a simple shell alias to your .bashrc or .zshrc to filter out all the .png, .jpg, .jpeg and .gif files from the rest allowing mat2 to only remove data from that files specifically.
Showing metadata
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | while IFS= read -r -d "" f; do mat2 -s "$f"; doneRemoving metadata
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | while IFS= read -r -d "" f; do mat2 --inplace "$f"; doneShell Script
This is the way I like to do it. Because it allows me to run the script from else where instead of cd in to the directory i want to remove metadata of.
#!/usr/bin/env bash
TARGET_DIR="$1"
OPTION="$2"
show_data() {
find "$TARGET_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | while IFS= read -r -d '' file; do
printf "\nShowing metadata of image %s\n" "$file"
mat2 -s "$file"
done
}
remove_data() {
find "$TARGET_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | while IFS= read -r -d '' file; do
printf "\nDeleting Metadata of image %s\n" "$file"
mat2 --inplace "$file"
done
}
if [ -z "${1:-}" ]; then
printf "\nError: No target directory was provided\n"
printf "\nPlease provide a target directory with cleaner target-directory\n"
exit 1
fi
if [ ! -d "$TARGET_DIR" ]; then
printf "\nThe directory %s is not found" "$TARGET_DIR"
exit 1
fi
case "${2:-}" in
-c)
remove_data
;;
-s)
show_data
;;
"")
printf "\nNo argument provided please provide an argument (-s or -c)\n"
exit 1
;;
*)
printf "\ninvalid option provided exiting the script\n"
exit 1
;;
esacThe script is pretty self explanatory with safety checks for checking whether directory already exists and null target or null option checks. The mat2 command efficiently removes all the metadata in the target directory.
Usage
- Remove metadata
./cleaner.sh <target-dir> -c- Check/show metadata
./cleaner.sh <target-dir> -sWhat to use
In my opinion using the shell script is much more safe because it has the safety controls such as checking whether directory already exists or not. But if you don’t care about the safety options or just don’t want a shell script, please add the aliases to your .bashrc or .zshrc to use it.

