TehOne
November 11, 2009, 10:56am
1
Hello,
I've got a script to delete 0 byte files, but I need it to work only for files that have been created at least 2 seconds ago (Are two seconds old).
I'm not sure what's the best way of doing this, I've had a look at the stat command too but well..
for file in `ls -l | grep ^- | awk '{print $5, $9}' | grep ^0 | awk '{print $2}'` ;do
chmod 777 $file >/dev/null 2>&1
rm -f $file >/dev/null 2>&1
done
mkastin
November 11, 2009, 11:24am
2
This should find and delete files older that 1 minute.
find /path/to/files -type f -mmin +1 -exec rm {} \;
You can simplify your ls: -
ls -l | nawk ' /^-/ && ($5 == "0"){print $NF}'
Rather than get involved in date arithmetic can you get away with a sleep of two seconds before doing the delete?
The best way of doing this is not doing it at all ....
Why create a file that needs to be immediately removed?
Anyway, you can play with something like this, but be careful,
it's very, very dangerous!
perl -e'
-z and (2/86400) < -M and unlink for @ARGV
' *
Use it at your own risk.
TehOne
November 11, 2009, 11:31am
6
That's not a solution and it's something I already knew just that 1 minut is not an option.
steadyonabix:
You can simplify your ls: -
ls -l | nawk ' /^-/ && ($5 == "0"){print $NF}'
Rather than get involved in date arithmetic can you get away with a sleep of two seconds before doing the delete?
A sleep won't do the job either, that's because the file can change it's size and sure I could check it's size after those two seconds but that's not like I want it.
radoulov:
The best way of doing this is not doing it at all ....
Why create a file that need to be immediately removed?
Anyway, you can play with something like this, but be careful,
it's very, very dangerous!
perl -e'
-z and (2/86400) < -M and unlink for @ARGV
' *
Use it at your own risk.
I need such a check to avoid a race-condition issue...
mkastin
November 11, 2009, 12:12pm
7
for file in `ls -ltr --time-style=+%s | awk '{now=systime(); del_time=now-2; if($6<del_time && $5=="0") print $7}'` ;do
chmod 777 $file >/dev/null 2>&1
rm -f $file >/dev/null 2>&1
done
As you haven't explained the race condition, I can only wonder if "lsof" might help.