Find out if file has been modified within the last 2 minutes

前端 未结 6 1720
栀梦
栀梦 2020-12-11 14:51

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 <

6条回答
  •  情深已故
    2020-12-11 15:08

    Here is a solution that will test if a file is older than X seconds. It doesn't use stat, which has platform-specific syntax, or find which doesn't have granularity finer than 1 minute.

    interval_in_seconds=10
    filetime=$(date -r "$filepath" +"%s")
    now=$(date +"%s")
    timediff=$(expr $now - $filetime)
    if [ $timediff -ge $interval_in_seconds ]; then
      echo ""
    fi
    

提交回复
热议问题