Visit Our UNIX and Linux User Community


rm files older then 2 seconds?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rm files older then 2 seconds?
# 1  
Old 11-11-2009
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..

Code:
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

# 2  
Old 11-11-2009
This should find and delete files older that 1 minute.

Code:
find /path/to/files -type f -mmin +1 -exec rm {} \;

# 3  
Old 11-11-2009
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?
# 4  
Old 11-11-2009
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.

Last edited by radoulov; 11-11-2009 at 02:35 PM.. Reason: spelling
# 5  
Old 11-11-2009
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...
# 6  
Old 11-11-2009
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:
Originally Posted by steadyonabix
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?
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.

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?

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.
I need such a check to avoid a race-condition issue...
# 7  
Old 11-11-2009
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


Last edited by mkastin; 11-11-2009 at 02:17 PM.. Reason: forgot the bytes=0 condition

Previous Thread | Next Thread
Test Your Knowledge in Computers #96
Difficulty: Easy
Unix and Unix-like operating systems are a family of computer operating systems that are derived from the original Unix System developed at Xerox Park.
True or False?

10 More Discussions You Might Find Interesting

1. AIX

Getting files through find command and listing file modification time upto seconds

I have to list the files of particular directory using file filter like find -name abc* something and if multiple file exist I also want time of each file up to seconds. Currently we are getting time up to minutes in AIX is there any way I can get file last modification time up to seconds. (4 Replies)
Discussion started by: Nitesh sahu
4 Replies

2. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

3. Shell Programming and Scripting

Archiving older files

Hello Group, I would request your help to build a shell script in order to find files older than 90 days then create the same directory structure under the second disk (/archive directory) and move the file preserving the same timestamps (ownership, etc). Also keep the log of files moved... (4 Replies)
Discussion started by: csierra
4 Replies

4. Shell Programming and Scripting

how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files.. Could u help me through this..?? (5 Replies)
Discussion started by: shaal89
5 Replies

5. Shell Programming and Scripting

files older than a certain time

I know how to find files, which are newer than a specific time. touch -t 201103300650 dummy find /path/to/files -type f -newer dummy -exec ls -l {} \; Is there a way to find files, which are older than a specific time? (2 Replies)
Discussion started by: BeefStu
2 Replies

6. Shell Programming and Scripting

files older than few hours

Hi All I need to know the command which can be used to list the files which are 3 hours old so that it can be deleted. (3 Replies)
Discussion started by: mskalyani9
3 Replies

7. Shell Programming and Scripting

Getting list of files generated last 10 seconds

I was trying to figure out in Korn shell but this may apply elsewhere how to generate a list of files from a directory created in the last 10 seconds or less. I have used the find command in the past with -mtime which is measured in days to get a list of files older than say 7 days for example. ... (1 Reply)
Discussion started by: lee_murray
1 Replies

8. UNIX for Dummies Questions & Answers

Remove the older files

Hi All, I need to remove some old files which the file creation date is older than a week. I've tried to use command: find . -atime +6 -exec rm{}. but it seems the creation date of files shown above were not as I expected. please your kind advice. Thanks. (1 Reply)
Discussion started by: Prasandha
1 Replies

9. UNIX for Dummies Questions & Answers

rm files older than ...

Hello, How can I remove files older than yesterday or the day before or a given day ... Thank you in advance (2 Replies)
Discussion started by: annemar
2 Replies

10. Shell Programming and Scripting

files older than 15 minutes

Hi Friends, i have to write a script to raise a flag if there are any files that are older than 15 minutes in the directory.The directory is supplied as the parameter to the script. please help with a sample script. Thanks in advance veera (0 Replies)
Discussion started by: sveera
0 Replies

Featured Tech Videos