I need to find the latest files that are accessed less than 10mins in a particular directory & send those files in an attachment.
I could use the below simple one. But if the directory was not updated any recently i could mail the old file again, i need to eliminate that.. What shld i do for this.
Well specifying the operating system and shell would be a start.
Check out find and atime (access time) option.
The filesystem mounted will need to have atime attribute on (most have by default), other don't due to possible performance reasons.
Well HPUX find does not support minutes with atime.
There is not stat either.
So either perl(stat) or C (fstat) to manipulate (older then) comes to mind.
I'll have to look into it to produce some code on HPUX box, have to get back to you tomorrow or next week.
As far as i get it, if you operate within same directory, the operation of 'sending those files' will change the atime again.
So next iteration of code will detected those again as accessed in the last 10 minutes (10 minutes or less).
This could be mitigated by issuing touch -at <TIMESTAMP> files to change all files sent 20 minutes behind script execution time.
This will, of course, fake atime on processed files.
As for -newer i'm not sure it will operate on atime or ?
Perhaps some awk parsing and ls -lutr ... shouldn't be to big.
I'll get back to you on that when i get to hpux box these days
I have written a code with the below concept. Does it make sense?
Step 1:getting the newest file from ls -lrt | tail -1 & sending the file as an email attachment ( This is my req. need to find the recently accessed file & send it as attachment in an email)
Step 2: Move that file in to a back up directory. So that same file will not get accessed in the next run
Repeating this step via script managed by a scheduler.
As a matter of fact: no. ls -t sorts according to modification time, not access time, so whatever the result may be it is bogus.
Alas, the -newer of find won't help either because it only takes modification time into account. The only way to do it is - as already has been suggested - to write a C program around the fstat() function or a perl program arount its respective function (or, perhaps, writing a program in another high-level language that provides a similar function in its library).
Taking the first file from ls -ut (with no need to throw away unneeded long listing information and using access time instead of modification time for sorting) instead of the last file modified from ls -lrt would seem to come closer to what you said you want to do and be both faster and easier.
But, of course, this gives you the most recently accessed file even if it hasn't been accessed for a week.
The find -newer path primary compares modification times instead of access times. The standard find primaries do not provide a way to search for files based on access times.