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
  if ! test `find $f -mmin +40`
    echo $f
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:

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.



Top comments (0)