Operating System - HP-UX
1833777 Members
2129 Online
110063 Solutions
New Discussion

Re: find content by second

 
elainelaw
Occasional Contributor

find content by second

I want to use the command "find" to search the file content in a directory , I know mmin can search by minute , mtime by date , if I want by second , what can I do ? thx
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: find content by second

Hi:

Use Perl;

This script will find files in the current directory that have been modified within the number of seconds passed as an argument:

# perl -MFile::Find -le '$secs=shift||60;find(sub{print $File::Find::name if -f $_ && -M _ <= $secs/86400},".")'

By default, 60-seconds is assumed. To look for files in '/tmp' modified in the last hour, you would do:

# cd /tmp;
# perl -M...)' 3600

Regards!

...JRF...
Tim Nelson
Honored Contributor

Re: find content by second

are you really looking for by 1 second ? Curious what you are going to do as the results would probably take at least another second to act upon.

Any way..

Here is a different twist.
Have your script create a file.
Then use find with the -newer option to find all files newer than the file you just created.
Go to sleep for 1 second
touch your file again
then find again.

e.g.
while true
do
> control.file
find ./ -newer control.file
sleep 1
done

I would expect this script to chow up CPU and disk resources depending on the size of the directory your are searching, and again, if it takes more than a second to do the search then output will be irrelevant. Maybe sleep 10 is better ?


haeman
Frequent Advisor

Re: find content by second

The reason that I want to do it is I would like to write a script to find a word "error" in a directory regularly ( some new files will write to this directory from time to time ) , then send the mail to administrator to report which file contain the word "error" , but I want the administrator should only receive the same file name one time , that means if the script found the same file have the word "error" , it do not send the report to administrator again for the same file name ) , my method is run a crontab job script ( find -name "error" , then send mail ...) in every 2 hours , to prevent the checking mistake if the checking and file generation are at the same time , so I want to change my script to ( find -name "error" -time 2880+5 , then send mail...) , can advise how to make use the script provided ?

# perl -MFile::Find -le '$secs=shift||60;find(sub{print $File::Find::name if -f $_ && -M _ <= $secs/86400},".")'



ps.
2 Hr = 2880 second
Rasheed Tamton
Honored Contributor

Re: find content by second

Hi,

There is no mmin option with the find in HP-UX.

You mean you will run the cron in every 2 hrs and will send the mail to the admin. So, how come the 1 second requirement. Can you please elaborate your requirement more in detail.

Rgds.


OldSchool
Honored Contributor

Re: find content by second

i think i'd be tempted to keep a list of all of the files already transmitted and when the job runs list all of the the files with "error" in them.

Transmit the difference in the lists and update the transmitted list.

Initialization of the "transmitted" list, and when this should be done are left as an excercise....