How can I tell if a file is older than 30 minutes from /bin/sh?
The following gives you the file age in seconds:
echo $(( `date +%s` - `stat -L --format %Y $filename` ))
which means this should give a true/false value (1/0) for files older than 30 minutes:
echo $(( (`date +%s` - `stat -L --format %Y $filename`) > (30*60) ))
30*60
-- 60 seconds in a minute, don't precalculate, let the CPU do the work for you!
If you're writing a sh script, the most useful way is to use test
with the already mentioned stat trick:
if [ `stat --format=%Y $file` -le $(( `date +%s` - 1800 )) ]; then do stuff with your 30-minutes-old $filefi
Note that [
is a symbolic link (or otherwise equivalent) to test
; see man test
, but keep in mind that test
and [
are also bash builtins and thus can have slightly different behavior. (Also note the [[
bash compound command).