EDIT:
I've improved this "FolderWatcher script" of user konsolebox (from the answer below: https://stackoverflow.com/a/18597233/2095175 ) I have added these lines to automatically move every kind of file in a right folder on my system (eg pdf in /Docs folder, Images in /Picture and video files in /Videos)
extension=${ADDED##*.}
if [ "$extension" = "xz" ] || [ "$extension" = "zip" ] || [ "$extension" = "gz" ] || [ "$extension" = "bz2" ] || [ "$extension" = "7z" ]; then
open $ADDED
fi
if [ "$extension" = "pdf" ] || [ "$extension" = "txt" ] || [ "$extension" = "odt" ] || [ "$extension" = "doc" ] ; then
mv "$ADDED" /boot/home/Docs
alert --idea " $ADDED moved to /boot/home/Docs"
open /boot/home/Docs
fi
if [ "$extension" = "jpg" ] || [ "$extension" = "png" ] || [ "$extension" = "gif" ]; then
mv "$ADDED" /boot/home/Media/Images
alert --idea " $ADDED moved to /boot/home/Media/Images"
open /boot/home/Media/Images
fi
if [ "$extension" = "flv" ] || [ "$extension" = "avi" ] || [ "$extension" = "mp4" ] || [ "$extension" = "mpg" ]; then
mv "$ADDED" /boot/home/Media/Video
alert --idea " $ADDED moved to /boot/home/Media/Video"
open /boot/home/Media/Video
fi
ORIGINAL QUESTION:
i have the following script which continuously check for the content of a folder, and alert me whenever a new file is inserted and/or when is deleted. Works perfectly as expected. But doesn't check subfolders in the main folder ( folder=$(cat /boot/home/FolderWatcher_pref.txt) ) Eg if I insert/remove file from the main folder, i will be alerted, but if i insert/remove files in a subfolder of "$folder", the script is not able to alert me. What can i change or add in this script to achieve this need?
I am on Haiku OS, so some command like "alert" are Haiku-specific.
#!/bin/bash cat /dev/null > $difffile1 cat /dev/null > $difffile2 #The path where to look is set in a text file, which i can change with a file panel to select any folder folder=$(cat /boot/home/FolderWatcher_pref.txt) tstamp=$(stat --print "%Y" "$folder") while true; do prev=$(ls "$folder" | tr '\n' '\n' > /tmp/prev.txt) sleep 5 if [[ "$folder" == "$folder" && $tstamp -lt $(stat --print "%Y" "$folder") ]]; then after=$(ls "$folder" | tr '\n' '\n' > /tmp/after.txt) difference1=$(comm -2 -3 "/tmp/after.txt" "/tmp/prev.txt">/tmp/Diff.txt) added=$(cat /boot/common/cache/tmp/Diff.txt) difference2=$(comm -2 -3 "/tmp/prev.txt" "/tmp/after.txt">/tmp/Diff2.txt) lost=$(cat /boot/common/cache/tmp/Diff2.txt) difffile1=/tmp/Diff.txt difffile2=/tmp/Diff2.txt FILESIZE2=$(stat -c%s "$difffile2") if [ "$FILESIZE2" == 0 ] then lost=nothing fi FILESIZE1=$(stat -c%s "$difffile1") if [ "$FILESIZE1" == 0 ] then added=nothing fi lost2=$(cat /boot/common/cache/tmp/Diff2.txt) alert --idea "$folder: $added *INSERTED*. $lost *REMOVED*."; echo "$lost2" >>$folder/Removed.txt tstamp=$(stat --print "%Y" "$folder") cat /dev/null > $difffile1 cat /dev/null > $difffile2 else sleep 3; fi done
prev=$(ls "$folder" | tr '\n' '\n' > /tmp/prev.txt)
. Interesting script by the way.. - konsolebox[ "$extension" = "xz" ] || [ "$extension" = "zip" ] || [ "$extension" = "gz" ] || [ "$extension" = "bz2" ] || [ "$extension" = "7z" ]; then
could just be simplified as[[ $extension == @(xz|zip|gz|bz2|7z) ]]
. You could also ignore the case by doingshopt -s nocaseglob
. And better quote your variable:open "$ADDED"
. I think everything's good with that already. - konsolebox