Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

Find Command

SOLVED
Go to solution
Highlighted
John Carver
Frequent Advisor

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
6 REPLIES
James R. Ferguson
Acclaimed Contributor

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...




Hein van den Heuvel
Honored Contributor

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

James R. Ferguson
Acclaimed Contributor
Solution

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...
John Carver
Frequent Advisor

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.
Dennis Handly
Acclaimed Contributor

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.
Hein van den Heuvel
Honored Contributor

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