Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

unix

sh

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?

like image 786
ph3nx Avatar asked Feb 05 '15 06:02

ph3nx


People also ask

How can you tell when the last time was modified?

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.

How do you check if a file has been modified?

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.

Which command shows files modified in last 5 minutes?

Syntax of find command with “-mmin n” option. Find files modified in last 5 minutes in Linux.

How do I find out where a file was modified today?

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.


2 Answers

I think this would be helpful,

find . -mmin -2 -type f -print 

also,

find / -fstype local -mmin -2 
like image 123
Skynet Avatar answered Sep 22 '22 15:09

Skynet


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.

like image 40
Aubrey Kilian Avatar answered Sep 24 '22 15:09

Aubrey Kilian