C chinab0wl macrumors newbie Original poster Apr 22, 2011 6 0 Apr 22, 2011 #1 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!
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!
G Guiyon macrumors 6502a Mar 19, 2008 771 4 Cambridge, MA Apr 22, 2011 #2 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: Apr 22, 2011
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?
C chinab0wl macrumors newbie Original poster Apr 22, 2011 6 0 Apr 22, 2011 #3 Guiyon said: 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 '{}' \; Click to expand... awesome! i didn't know about those find parameters. thank you for the enlightenment
Guiyon said: 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 '{}' \; Click to expand... awesome! i didn't know about those find parameters. thank you for the enlightenment