In a bash script I want to check if a file has been changed within the last 2 minutes.
I already found out that I can access the date of the last modification with stat file.ext -c %y
. How can I check if this date is older than two minutes?
Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.
You can use the stat command on a file to check access and modification times or set up RCS to track changes. You can use MD5 or sum to get the current state of the file, copy that value to a file and then that file to verify that the original file wasn't changed.
Syntax of find command with “-mmin n” option. Find files modified in last 5 minutes in Linux.
How to find the date of modified files. Press the Windows key + E on the keyboard to open File Explorer. On the left side-scrolling menu, select the drive or folder that you want to view the last modified date(s) (A) for the contents.
I think this would be helpful,
find . -mmin -2 -type f -print
also,
find / -fstype local -mmin -2
Complete script to do what you're after:
#!/bin/sh # Input file FILE=/tmp/test.txt # How many seconds before file is deemed "older" OLDTIME=120 # Get current and file times CURTIME=$(date +%s) FILETIME=$(stat $FILE -c %Y) TIMEDIFF=$(expr $CURTIME - $FILETIME) # Check if file older if [ $TIMEDIFF -gt $OLDTIME ]; then echo "File is older, do stuff here" fi
If you're on macOS
, use stat -t %s -f %m $FILE
for FILETIME
, as in a comment by Alcanzar.
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