get directory mtime in unix time

In scripts when you need to compare last modification date of directories, you can get the date using stat in a unix timestamp or seconds from the Epoch:

# stat -c '%Z' /usr/local/sbin

Using date you can get the same format like this:

# date +%s

You could use this in a script to do something if a directory is older or newer than some amount of time:

# AUTHOR: ForDoDone <fordodone at>
# DATE: 2013-07-12
# NOTES: syncs /usr/local/sbin to hostxyz if it's been modified in the last 5 minutes

now=`date +%s`

uls_lastmtime=`stat -c '%Z' /usr/local/sbin`

uls_diff=$(echo $now - $uls_lastmtime |bc)

if [ $uls_diff -lt 300 ]
  rsync -a /usr/local/sbin/ hostxyz:/usr/local/sbin

Of course rsync has a bunch of options to check whether it needs to do an update of files, this is just an example.

Leave a Reply

Your email address will not be published. Required fields are marked *