I want to find if certain files are changed within the last three minutes in order to decide if the cp
was successful, and if I should exit
or continue the script. How can I do that?
Thanks
The syntax of this if statement depends on your particular shell, but the date commands don't. I use bash; modify as necessary.
if [ $(( $(date +%s) - $(date +%s -r <file>) )) -le 180 ]; then
# was modified in last three minutes
else
# was not modified in last three minutes
fi
The +%s
tells date to output a UNIX time (the important bit is that it's an integer in seconds). You can also use stat to get this information - the command stat -c %Y <file>
is equivalent. Make sure to use %Y
not %y
, so that you get a usable time in seconds.
Find can output the file name if it has been modified in the last 3 minutes.
find file1 -maxdepth 0 -mmin -3
You can get the last modification time of a file with stat
, and the current date with date
. You can use format strings for both to get them in "seconds since the epoch":
current=`date +%s`
last_modified=`stat -c "%Y" $file`
Then, it's pretty easy to put that in a condition. For example:
if [ $(($current-$last_modified)) -gt 180 ]; then
echo "old";
else
echo "new";
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With