mouthpiec
mouthpiec

Reputation: 4043

Linux Script to get time in seconds when folder was last modified

I need a linux bash script to know the time in seconds when a particular folder was modified. Can someone please help me?

My current script below is getting the current time stamp and the last time the folder was modified, but i do not know how to proceed.

[root@cgf01 log]# more CheckLastCdr.sh 
#get current timestamp
current_time=`date` 

#get last CDR timestamp
last_cdr_time=`find /tmp/log/ -exec stat \{} --printf="%y\n" \; | sort -n -r | head -1`


echo $current_time
echo $last_cdr_time

when i run this script i am getting the following:

[root@cgf01 log]# ./CheckLastCdr.sh 
./CheckLastCdr.sh: line 6: 2011-04-05: command not found
Tue Apr 5 16:19:31 CEST 2011
2011-04-05 16:14:33.000000000 +0200

thanks in advance

Upvotes: 0

Views: 5201

Answers (3)

pajton
pajton

Reputation: 16246

Change:

find /tmp/log/ -exec stat \{} --printf="%y\n"

to:

find /tmp/log/ -exec stat \{} --printf="%T@\n"

Upvotes: 0

mvds
mvds

Reputation: 47134

If you want the number of seconds ago:

echo $[$(date +%s)-$(stat --printf "%Y" /tmp/log)]

Upvotes: 3

drysdam
drysdam

Reputation: 8637

You don't have to use find to get this info. You can use stat like so:

stat --printf=%Y dirname

Upvotes: 1

Related Questions