how do I check in bash whether a file was created more than x time ago?

后端 未结 8 1200
时光说笑
时光说笑 2020-12-22 18:24

I want to check in linux bash whether a file was created more than x time ago.

let\'s say the file is called text.txt and the time is 2 hours.

 if [         


        
相关标签:
8条回答
  • 2020-12-22 19:29

    Only for modification time

    if test `find "text.txt" -mmin +120`
    then
        echo old enough
    fi
    

    You can use -cmin for change or -amin for access time. As others pointed I don’t think you can track creation time.

    0 讨论(0)
  • 2020-12-22 19:30

    The find one is good but I think you can use anotherway, especially if you need to now how many seconds is the file old

    date -d "now - $( stat -c "%Y" $filename ) seconds" +%s

    using GNU date

    0 讨论(0)
提交回复
热议问题