Comparing file timestamps

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

  1. chinab0wl macrumors newbie

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

    Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #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?
     
  3. chinab0wl thread starter macrumors newbie

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

Share This Page