This script was needed to monitor product images on a Magento 2 store. We noticed that sometimes after importing product updates, images which had been working were now missing.

The Magento products were imported using a custom extension from a CSV file which included fields for product images. Images specified in the CSV fields needed to be uploaded to a staging folder on the website so the import process could find and move them to the correct path for Magento to use.

The CSV was created using a back-end process which got product details from a database and added all images found in the back-end master directory to each product.

If an image had been manually added to a product using the Magento admin pages instead of the CSV it would work fine. However, if the image hadn’t also been uploaded to the staging directory, the next time that same product was updated from the CSV, the problem occurred. Any images not found in the staging directory would be removed from the Magento product DB and filesystem.

This script was scheduled as a daily cron job to monitor the Magento image directory and notify by email if any images had been deleted (or added) since the last time it ran.

Note that this Magento 2 installation used an additional non-standard location to store extra product images (pub/media/catalog/history).

Tested on Ubuntu 18.0.4 LTS

#!/usr/bin/env bash

# based on a script from

# Directory you want to watch

# Sub-directory 1 you want to exclude

# Sub-directory 2 you want to exclude

# Sub-directory 3 you want to exclude

# Name of the file that will keep the list of the files when you last checked it

# Name of the file that will keep the list of the files you are checking now

# Name of the file to store text for an email

# The first time we create the log file
touch $LAST

find $WATCHDIR -path $EXCLUDEDIR -prune -o -path $EXCLUDEDIR2 -prune -o -path $EXCLUDEDIR3 -prune -type f -o -print > $CURRENT

diff $LAST $CURRENT > /dev/null 2>&1

# If there is no difference exit
if [ $? -eq 0 ]
    echo "No changes"
    # Else, list the files that changed

    echo "List of new files" >$EMAILTEXT
    diff $LAST $CURRENT | grep '^>' >>$EMAILTEXT
    echo "List of files removed" >>$EMAILTEXT
    diff $LAST $CURRENT | grep '^<' >>$EMAILTEXT

    echo "$(cat $EMAILTEXT)"

    # Send a notification
    mail -r "" -s "Website images check" <$EMAILTEXT

    # Lastly, move CURRENT to LAST

Example of the email notification

List of new files
List of files removed
< /var/www/vhosts/