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

我的未来我决定 提交于 2019-12-01 15:04:10

I think this would be helpful,

find . -mmin -2 -type f -print

also,

find / -fstype local -mmin -2
Aubrey Kilian

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.

I solved the problem this way: get the current date and last modified date of the file (both in unix timestamp format). Subtract the modified date from the current date and divide the result by 60 (to convert it to minutes).

expr $(expr $(date +%s) - $(stat mail1.txt -c %Y)) / 60

Maybe this is not the cleanest solution, but it works great.

Here is how I would do it: (I would use a proper temp file)

touch -d"-2min" .tmp
[ "$file" -nt .tmp ] && echo "file is less than 2 minutes old"

here's an even simpler version that uses shell math over expr:

SECONDS (for idea)

echo $(($(date +%s) - $(stat file.txt  -c %Y)))

MINUTES (for answer)

echo $(($(($(date +%s) - $(stat file.txt  -c %Y))) / 60))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!