Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chinab0wl

macrumors newbie
Original poster
Apr 22, 2011
6
0
I'm trying to compare a file's mtime to the current time via command line. Does anyone know how I can do this? The goal is to be able to have a script that will delete certain files that have a mtime greater than 24 hours. Thanks!
 
Is there a huge need to write your own script to do this? find is capable of doing it:

Code:
find . -not -newermt "24 hours ago" -exec rm '{}' \;

If you you really want to just do a comparison in the script, then you could always use stat and date and do something like this:

Code:
MTIME=`stat -f "%m" /path/to/my/file`
CTIME=`date "+%s"`
DIFF=$(( ${CTIME} - ${MTIME} ))

if [[ ${DIFF} -ge 86400 ]]; then
    echo "File was last modified $(( ${DIFF} / 60 / 60 )) hours ago"
else
    echo "File mtime is less than 24 hours"
fi%

Anyone else have some other (or better) ideas?
 
Last edited:
Is there a huge need to write your own script to do this? find is capable of doing it:

Code:
find . -not -newermt "24 hours ago" -exec rm '{}' \;

awesome! i didn't know about those find parameters. thank you for the enlightenment :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.