Test a file date with bash Test a file date with bash bash bash

Test a file date with bash


I'm afraid I cann't answer the question for creation time, but for last modification time you can use the following to get the epoch date, in seconds, since filename was last modified:

date --utc --reference=filename +%s

So you could then so something like:

modsecs=$(date --utc --reference=filename +%s)nowsecs=$(date +%s)delta=$(($nowsecs-$modsecs))echo "File $filename was modified $delta secs ago"if [ $delta -lt 120 ]; then  # do somethingfi

etc..

UpdateA more elgant way of doing this (again, modified time only): how do I check in bash whether a file was created more than x time ago?


Here is the best answer I found at the time being, but it's only for the modification time :

expr `date +%s` - `stat -c %Y /home/user/my_file`


If your system has stat:

modsecs=$(stat --format '%Y' filename)

And you can do the math as in Joel's answer.