DEV Community

Timothy Huang
Timothy Huang

Posted on

Check if file modified in n minutes

Sometime we want to check if file modified in n minutes and do something with each file, we can easily use some commands.

Use test to check if condition, find with -mmin to check if older than n minutes, and for-do-done loop to enumerate each file in *.txt

Ok, let's put all together,

# check if file modified in 40 mins
for f in *.txt
do
  if ! test `find $f -mmin +40`
  then
    echo $f
  fi
done
Enter fullscreen mode Exit fullscreen mode

for f in *.txt is to enumerate each file with txt extension file, and if ! is for check the false condition with find $f -mmin +40, i.e. $f not older than 40 minutes, also means $f is as new as modified in 40 minutes.

There is a example test code to check:

https://repl.it/@timhuangt/checkIfFileModified

Just create 3 files (with touch command) that modified in now, 30 minutes ago, 60 minutes ago, and with the modified date-time filename with each file. After the for-do-done loop, we can echo the file(s) name modified in 40 minutes.

Reference:

  1. https://stackoverflow.com/questions/552724/how-do-i-check-in-bash-whether-a-file-was-created-more-than-x-time-ago
  2. https://unix.stackexchange.com/questions/118577/changing-a-files-date-created-and-last-modified-attributes-to-another-file/250407
  3. https://stackoverflow.com/questions/16142973/add-some-specific-time-while-using-the-linux-command-date

Top comments (0)