This post is updated, look below for the solution.
i have the need to check a folder for a presence of a file, which is not always present.
i made a script like this:
#!/bin/sh while true; do file=/path/to/file if [[ "$file" = *filename* ]] then echo "$file is present" else echo "No present" fi sleep 3 done
Works perfectly, except for the fact that the "$file is present" is continuosly repeated, until i delete or move the file. Which command can I insert after "echo "$file is present" to stop the alert but continue to check for this file (eg when the file will be again available) ?
Thank you.
Since i can't add an answer until 8 hours, i post here my solution:
In anyway i have solved using this script: comparing the time of the file with the current date and then, using "touch" changing the date to 10 seconds ago:
#!/bin/sh while true do cd $(dirname "$0") current=$(pwd) cd /boot/home/Downloads last=$(ls -t | head -n1) name=$(basename "$last") filedate=$(date -r "$last" +%G%m%d%H%M%S) currentdate=$(date +%G%m%d%H%M%S) if [ "$filedate" -eq "$currentdate" ] then echo "$name" "is present" touch -d '-10 seconds' "$name" fi done
Now works as espected and indipendently from the file name! Alert me about every new file just once and keep to watch that folder :-)
To keep the whole history of script, below there is the script of iamauser. I have a little bit improved this script: now it alert me for every new file (indipendentely from name, kind and so on) inside a folder and also alert me for deleted files :-)
#!/bin/bash cd /boot/home filename=$(ls -t | head -n1) tstamp=$(stat --print "%Y" "$filename") while true; do prev=$(ls "/boot/home/Downloads" | tr '\n' '\n' > /tmp/prev.txt) check=$(ls -t /boot/home/Downloads | head -n1) if [ ! -d "$filename" ]; then after=$(ls "/boot/home/Downloads" | tr '\n' '\n' > /tmp/after.txt) echo "Not present"; sleep 5; elif [[ "$filename" == "$filename" && $tstamp -ne $(stat --print "%Y" "$filename") ]]; then sleep 2 difference=$(comm -2 -3 "/tmp/after.txt" "/tmp/prev.txt">/tmp/Diff.txt) lost=$(cat /boot/common/cache/tmp/Diff.txt) alert --idea "/boot/home/Downloads: $check is the most recent file in this folder."; alert --idea "/boot/home/Downloads: $lost removed."; tstamp=$(stat --print "%Y" "$filename") else sleep 3; fi done
sleep 10
). - konsolebox