I am trying to use inotifywait to watch a folder (/shares/Photos) and when it detects a jpg added to the folder I need to resize it into a sub directory ($small_dir). Under the photos director there will be many subfolders for the jpgs.
The tree looks like this
shares
-Photos
-Folder 1
-Folder 2
.
.
.
Basically whenever someone copies pictures into folder 1 I need to create a new subfolder, and then resize the images and put the smaller versions into that folder.
So the tree would become:
shares
-Photos
-Folder 1
-Resized
-Folder 2
.
.
.
My code so far:
inotifywait -mr --timefmt '%m/%d/%y %H:%M' --format '%T %w %f' -e close_write /shares/Photos --includei "\.jpg|\.jpeg" |
while read -r date time dir file; do
changed_abs=${dir}${file}
small_dir=${dir}${target}/
printf "\nFile $changed_abs was changed or created\n dir=$dir \n file=$file \n small_dir=$small_dir \n"
# Check if the $small_directory exists, if not create it.
if [ -d "$small_dir" -a ! -h "$small_dir" ]
then
echo "$small_dir found, nothing to do."
else
echo "Creating $small_dir"
mkdir $small_dir
chmod 777 $small_dir
fi
# Check to see if the file is in $small_dir, if it is, do nothing.
if [ "$dir" = "$small_dir" ]; then
printf "\nFile is in the $small_dir folder, nothing to do\n"
else
printf "\nResizing file into the $small_dir folder\n"
# Code to resize the image goes here.
fi
done
It mostly works, but what I am banging my head against the wall about is that if I create a new subfolder under Photos while the script is running, inotifywait simply ignores it and does nothing.
I tried replacing close_write with create but it made no difference, and I am really not sure where to go from here.
Any advice/help would be greatly appreciated.
inotifywaitignoring all new sub directories. I'll rework this after I fix the new director issue.