Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting old logfiles 1

Status
Not open for further replies.

deeway

MIS
May 8, 2006
16
0
0
NL
Hi,

I'm trying to make a script that deletes old logfiles on AIX 4.3.3.
How can i delete files older than 5 days?

Thnx
 
deeway,

Have a look at the command 'find' using the command:

man find | pg

There are some examples in the man page that will do almost exactly what you want. Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Hi,

Thanks for the tip.
I've tried it but no luck.

find /$PATH -atime +5 >logfile.log
if [ -n /$PATH/logfile.log ] ; then
rm ?????
fi

So how do i get unix to every line in the logfile and delete it?
 
Hi:

find doesn't work quite this way. Are you trying to search root or the
directories in $PATH?

If you have to give find a list of directories to search with $PATH, I think you can
do that by replacing ":" with a space:

dirs=$(echo $PATH|sed 's/:/ /g')

find $dirs -type f -atime +5 -print

There is no need to check if logfile.log exists. Once you have the command
the way you want it, pipe it to xargs and remove:

# untested
find $dirs -type f -atime +5 -print|xargs rm

Regards,


Ed
 
Hi:

Let me update my previous post. I'm assuming your file log files are called logfile.log.

find doesn't work quite this way. Are you trying to search root or the directories in $PATH?

If you have to give find a list of directories to search with $PATH, I think you can do that by replacing ":" with a space:

dirs=$(echo $PATH|sed 's/:/ /g')

find $dirs -type f -name "logfile.log" -atime +5 -print

There is no need to check if logfile.log exists. Once you have the command the way you want it, pipe it to xargs and remove:

# untested
find $dirs -type f -name "logfile.log" -atime +5
-print|xargs rm

Regards,


Ed
 
Question for Ed,

Is there a reason you prefer xargs to find's own -exec rm {} \; ?

I use xargs for trickier things (like doing a grep on all the XML files in the subdirectories of /usr/local). But for a simple delete, wouldn't -exec rm {} \; do the trick?

Help me out of my ignorance here. Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
Hi,
thanks for the response y'all.
The "find $dirs -type f -name "logfile.log" -atime +5
-print|xargs rm"command did it.
But i have a question for Ed.
I don't quit get the "dirs=$(echo $PATH|sed 's/:/ /g')
" command.
Can you explain?

PS: And a anwser on Einstein47's question for you Ed.
 
Hello Einstein:

If by "simple" you mean deleting just a few files, then I agree with you -exec rm {} \; should work OK.

In the early days of Unix (especially SCO Xenix) it was easy to overflow the command line buffer. The book Unix Power Tools, Articles 10.18 to 10.21, covers the overflow problem extensively.

Modern *NIXs do not over flow as easily, but, besides force of habit, these are the main reasons I still use xargs:

1) Especially if I'm doing something destructive like a move, I like to see the output of the find command. It's just as easy to pipe to xargs than fill in -exec.

2) Although I've really never run any personal tests, using xargs should be more efficient. If find discovers 10 objects, xargs executes rm only once, and not 10 times - once for each object.

3) Also, xargs can extend the functionality of the find:

find . -type f -name "*.txt" | xargs -I {} ksh -c "echo deleting {}; rm {}"

The above command finds all txt files, and echos a deleting message as each file is removed. The xargs -I option replaces {} in the string with the results of find.

I don't think I've covered anything here that others is this forum haven't already said about xargs, but I hope this answer yours question.

Regards,

Ed


 
Hi Deeway:

>I don't quit get the "dirs=$(echo $PATH|sed 's/:/ /g')

The find command allows searching multiple directories:

find /usr/bin /bin /etc -type f ....

I want to search every directory in PATH; Since the PATH variable is colon delimited, I can get a list of all the directories by globally substituting a space for ":". Therefore:

/usr/bin:/bin:/etc

becomes:

/usr/bin /bin /etc

which is the contents of the 'dirs' variable.

Regards,

Ed
 
Thanks for the explination Ed. I can see that you have earned your "old" title. The star
star.gif
is for your patience with us "young whipper/snappers". (-: Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
Hi Einstein:

Thanks for the kind words. It's appreciated - especially on a sad day as 9-11.

You know what they say about growing "old":

Growing old is mandatory; growing up is optional.

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top