Determining age of a file in shell script

后端 未结 6 1095
暖寄归人
暖寄归人 2021-01-04 18:28

G\'day,

I need to see if a specific file is more than 58 minutes old from a sh shell script. I\'m talking straight vanilla Solaris shell with some POSIX extensions i

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 18:44

    I needed something to test age of a specific file, to not re-download too often. So using GNU date and bash:

    # if file's modtime hour is less than current hour:
    [[ $(date +%k -r GPW/mstall.zip) -lt $(date +%k) ]] && \
    wget -S -N \
    http://bossa.pl/pub/metastock/mstock/mstall.zip \
    

    Update--this version works much better for me, and is more accurate and understandable:

    [[ $(date +%s -r mstall.zip) -lt $(date +%s --date="77 min ago") ]] && echo File is older than 1hr 17min
    

    The BSD variant (tested on a Mac) is:

    [[ $(stat -f "%m" mstall.zip) -lt $(date -j -v-77M +%s) ]] && echo File is older than 1hr 17min
    

提交回复
热议问题