Posts Topics Forums Images
Search videos from message boards Videos Search messages from microblogs Microblogs Search messages from imdb.com Imdb Search messages from yuku.com Yuku Search messages from lefora.com (free forums) Lefora
My account: Login | Sign Up
Loading... 

Thread: rm files older then 2 seconds?

Started 1 month ago by TehOne
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.. Code: for file in `ls -l | grep ^- | awk '{print $5, $9}' | grep ^0 | awk '{print $2}'` ;do chmod 777 $file >/dev/null 2>&...
Site: The UNIX Forums - the Top UNIX & Linux Q&A on the Web  The UNIX Forums - the Top UNIX & Linux Q&A on the Web - site profile
Forum: Shell Programming and Scripting  Shell Programming and Scripting - forum profile
Total authors: 5 authors
Total thread posts: 8 posts
Thread activity: no new posts during last week
Domain info for: unix.com

Other posts in this thread:

mkastin replied 1 month ago
Code: 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

mkastin replied 1 month ago
This should find and delete files older that 1 minute. Code: find /path/to/files -type f -mmin +1 -exec rm {} \;

steadyonabix replied 1 month ago
You can simplify your ls: - Code: 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?

radoulov replied 1 month ago
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! Code: perl -e' -z and (2/86400) < -M and unlink for @ARGV ' * Use it at your own risk.

mkastin replied 1 month ago
Quote: Originally Posted by radoulov The best way of doing this is not doing it at all .... Why create a file that need to be immediately removed? Couldn't agree more...

TehOne replied 1 month ago
Quote: Originally Posted by mkastin This should find and delete files older that 1 minute. Code: find /path/to/files -type f -mmin +1 -exec rm {} \; That's not a solution and it's something I already knew just that 1 minut is not an option. Quote: ...

TonyLawrence replied 1 month ago
As you haven't explained the race condition, I can only wonder if "lsof" might help.

 

Top contributing authors

Name
Posts
mkastin
3
user's latest post:
rm files older then 2 seconds?
Published (2009-11-12 06:50:00)
Code: for file in `ls -ltr --time-style=+%s | awk '{now=systime(); del_time=now-2; if($6&lt;del_time &amp;&amp; $5==&quot;0&quot;) print $7}'` ;do chmod 777 $file &gt;/dev/null 2&gt;&amp;1 rm -f $file &gt;/dev/null 2&gt;&amp;1 done
TehOne
2
user's latest post:
rm files older then 2 seconds?
Published (2009-11-12 06:50:00)
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.. Code: for file in `ls -l | grep ^- | awk '{print $5, $9}' | grep ^0 | awk '{print $2}'` ;do chmod 777 $file &gt;/dev/null 2&gt;&amp;1 rm -f...
steadyonabix
1
user's latest post:
rm files older then 2 seconds?
Published (2009-11-12 06:50:00)
You can simplify your ls: - Code: ls -l | nawk ' /^-/ &amp;&amp; ($5 == &quot;0&quot;){print $NF}' Rather than get involved in date arithmetic can you get away with a sleep of two seconds before doing the delete?
radoulov
1
user's latest post:
rm files older then 2 seconds?
Published (2009-11-12 06:50:00)
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! Code: perl -e' -z and (2/86400) &lt; -M and unlink for @ARGV ' * Use it at your own risk.
TonyLawrence
1
user's latest post:
rm files older then 2 seconds? -...
Published (2009-11-12 06:50:00)
As you haven't explained the race condition, I can only wonder if &quot;lsof&quot; might help.

Related threads on "The UNIX Forums - the Top UNIX & Linux Q&A on the Web":

Related threads on other sites:

Thread profile page for "rm files older then 2 seconds?" on http://www.unix.com. This report page is a snippet summary view from a single thread "rm files older then 2 seconds?", located on the Message Board at http://www.unix.com. This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity