Re: Find Command (144 Views)
Reply
Frequent Advisor
John Carver
Posts: 57
Registered: ‎10-23-2000
Message 1 of 7 (144 Views)
Accepted Solution

Find Command

I am writting debug logs to the /tmp directory for an application fix. These logs fill up the /tmp directory very quickly and I only need logs for the last 2 hours at any given moment in time. I want to include the find command in a script that continuously runs every 15 minutes and finds files with a specific name older than 2 hours and deletes them. I've been running the example below for awhile. It deletes files older than 1 hour and runs every hour so I have files anywhere between 1 and 2 hours old. Now I need to run every 15 minutes but delete files older than 2 hours. In other words, I need to retain roughly 2 hours of logs at all times.

I can't see if there's a way to do this with find or if I have to resort to something else.

#!/usr/bin/sh

while true
do
find /tmp -name "udapiserver*" ! -newer /tmp/findref -exec rm {} \;
touch /tmp/findref
sleep 3600
done

exit
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 7 (144 Views)

Re: Find Command

Hi:

One way is to use a small Perl snippet :

...
WHEN=$(perl -MPOSIX -e 'print strftime "%Y%m%d%H%M\n",localtime(time-(2*60*60))')
touch -amt ${WHEN} /tmp/findref
...

Now, your 'findref' file has a modification timestamp two hours ago (2*60*60 seconds ago) and you can awaken an compare to it as you need.

Regards!

...JRF...




Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 3 of 7 (144 Views)

Re: Find Command



Since you are not asking anything tricky from find (and even if you did), you may want to consider to have a shell or perl script do a glob and look at the date attribute to decide whether a file is a keeper or not.


for example:

$ perl -e 'for () { unlink if -M $_ > 2/24 }'

The -M function returns the age of the file as a floating point value with unit days.

So for 2 hours you want to compare using 2/24


hth,
Hein

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 7 (144 Views)

Re: Find Command

Hi (again) :

By the way, if you want to avoid poor 'find' performance, use the '+' terminator to the '-exec' argument like:

# find /tmp -type f -name "udapiserver*" ! -newer /tmp/findref -exec rm {} +

This causes multiple arguments to be collected for every 'rm' process spawned instead of forking one 'rm' for every file to be removed.

Regards!

...JRF...
Frequent Advisor
John Carver
Posts: 57
Registered: ‎10-23-2000
Message 5 of 7 (144 Views)

Re: Find Command

Thanks James!

I've incorporated the Perl snippet and the ref file has a timestamp exactly two hours old. I've also taken note of the "+" terminator, I did not know this.

Hein,

I did not try your suggested syntax, but will when I get the chance. Thanks for the response.
Acclaimed Contributor
Dennis Handly
Posts: 25,296
Registered: ‎03-06-2006
Message 6 of 7 (144 Views)

Re: Find Command

>I only need logs for the last 2 hours at any given moment in time. I want to include the find command in a script that runs every 15 minutes

If you don't want to use the perl solution to create the reference files, you'll need to have a sequence of 8 reference files and use and touch the appropriate one each 15 minutes.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 7 of 7 (144 Views)

Re: Find Command

>> I did not try your suggested syntax, but will when I get the chance.


Try a non-destructive version first.

Just after your purge runs, rerun the find, and compare with output from:

perl -e 'for () {$m = -M; print qq($m $_\n) if $m > 2/24 }'

That would just be to get familiar with this method.

for () {...} # walk list, setting $_, executing code block each time
# glob list of filenames matching specification
$m = -M; # store file modification date in varariable $m

print qq($m $_\n) if $m > 2/24 # real work.

hth,

Hein


The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.