Problem
(paraphrased/untested) : – find . -type f -mtime xx -exec rm {}\;
doesn’t always have a high enough resolution when you want to remove a file
that is seconds or minutes old.
In my application, I have “guest writers” who preview their web content
before submitting for review. I don’t want web robots to index this content
(so will have a meta in the html to that effect anyway) and I don’t want
nosy readers of my sites to be familiar with tmp files which are under the
wwwroot so they can read stuff that has yet to be approved AND also I want to
do good housekeeping and remove tmp content fairly quickly.
I could use a “staging” web server, after they author presses the
preview button, a temp web page is rendered. I can delete this page within
seconds of it being viewed – this is fine.
Solution
The following code will be run in cron, maybe every half an hour
Where
time resolution is in seconds thus the division by 60 to get minutes
bash does not do natively division hence the pipe to bc
the file is the modify time
#!/bin/bash
# echo " (`date +%s` - `stat -c %Z 12345`) / 60" | bc -l
for i in `ls -b $FILEPATH`;
do
U=$(( (`date +%s` - `stat -c %Z $i`) / 60 | bc -l ))
if [ $U -gt 30 ]; then
echo "$i is too old - will delete $U"
# insert the DANGEROUS bit here
else
echo "too young - will not delete $U";
# no dangerous bit here
fi
done