Reputation: 1172
There are many questions on how to delete files older than x minutes/hours/days on linux, but no one get to the seconds resolution.
I've found this solution:
for file in `ls -ltr --time-style=+%s | awk '{now=systime(); del_time=now-30; if($6<del_time && $5=="0") print $7}'` ;do
rm -f $file >/dev/null 2>&1
done
But systime()
is not present on awk
"function systime never defined"
but is on gawk
which I couldn't install on Ubuntu 13.xx (and really don't whant to install any extra software).
Upvotes: 8
Views: 5538
Reputation: 1172
A solution I found is to replace systime()
command, and use date +%s
like this:
for file in $(ls -ltr --time-style=+%s | awk '{cmd="date +%s"; cmd|getline now; close(cmd);del_time=now-30; if($6<del_time) print $7}') ;do
rm -f $file >/dev/null 2>&1
done
The trick is to capture the output of date +%s
inside awk
and using cmd="date +%s"; cmd|getline now; close(cmd)
was the only (really the first) way that I've found.
Edit: I changed backticks to parentheses, as recomended by @Jotne
Upvotes: -1
Reputation: 1773
Parsing output of ls
is always a bad approach. Especially when find
from GNU Findutils is able to do all the work by itself:
$ find -not -newermt '-30 seconds' -delete
Upvotes: 18