Comparing file timestamps

Discussion in 'macOS' started by chinab0wl, Apr 22, 2011.

  1. chinab0wl macrumors newbie

    Apr 22, 2011
    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!
  2. Guiyon, Apr 22, 2011
    Last edited: Apr 22, 2011

    Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    Is there a huge need to write your own script to do this? find is capable of doing it:

    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:

    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"
        echo "File mtime is less than 24 hours"
    Anyone else have some other (or better) ideas?
  3. chinab0wl thread starter macrumors newbie

    Apr 22, 2011
    awesome! i didn't know about those find parameters. thank you for the enlightenment :)

Share This Page