rm files older then 2 seconds?

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

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.

Couldn't agree more...

That's not a solution and it's something I already knew just that 1 minut is not an option.

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.

I need such a check to avoid a race-condition issue...

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.