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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete log files 1

Status
Not open for further replies.

tempest92

Technical User
Sep 16, 2003
110
GB
hi

i need to housekeep log files over a certain age, the script currently running reads a properties file that has entries such as -

/dir1/dir2/dir3/*txt 5
/dir1/dir2/dir3/*.txt 5

currently the script uses find i.e.
find /dir1/dir2/dir3/*txt -type f -mtime +5 -exec rm -e {} ;

the find is failing due to the arg list being too long. i cannot change the properties file as there is hundreds of entries in each and hundreds of servers !! can someone suggest a way (i guess using xargs) to get around this.

thanks in advance (i am using AIX)
 
find / -name core -exec 'rm -f {} ;'

is the same as

find / -name core | xargs rm -f

so

find /dir1/dir2/dir3/*txt -type f -mtime +5 | xargs rm -f

should do the trick

test with

find /dir1/dir2/dir3/*txt -type f -mtime +5 | xargs ls -l



Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
#find /dir1/dir2/dir3/*txt -type f -mtime +5 | xargs ls -l
ksh: /usr/bin/find: 0403-027 The parameter list is too long
 
Try the -n flag with xargs

see man xargs

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
thanks feherke. that works great !!

can you also tell me how i split

i=/dir1/dir2/dir3/*txt into -

dir=/dir1/dir2/dir3
file=*.txt

 
file=$(basename $i)
dir=$(dirname $i)

But be very careful when using variables containing wildcards in shell scripts!


HTH,

p5wizard
 
One handy workaround which I use for a similar script is to set -f to turn off filename generation.

Annihilannic.
 
just found set -f !

thanks everyone, this is a great help
 
Hi Anni,

Can you tell me what you mean by 'turn off filename generation' please ?

by example may be ?

thanks
 
By default (f flag off) the shell generates filenames with wildcard characters (* ? [] ...), so that is why it is sometimes awkward to include thoes special chars in the value of a variable. By setting the f flag on in a shell, you get rid of the special meaning of these wildcard characters and they behave just like any other character.

HTH,

p5wizard
 
By example:

[tt]$ touch one two three
$ echo *
one three two
$ set -f
$ echo *
*
$ set +f
$ echo *
one three two
$[/tt]

Annihilannic.
 
why does -
find /dir1/dir2/dir3/ -name \*txt -type f -mtime +5 | xargs rm -f work from the command line

but only -
find /dir1/dir2/dir3/ -name *txt -type f -mtime +5 | xargs rm -f if you are running from a script ?
 
thanks againg.... i am not having a good day !!

can you tell me how to get the \ into the second find to ensure it behaves the same whe variables are used as when it is hardcoded (obviously i won't have both finds, just the one with the variables)

#!/usr/bin/ksh

while read fname retain
do
set -f
file=$(basename $fname)
dir=$(dirname $fname)
set +f

find /dir1/dir2 -name \*log -type f -mtime +28 -exec ls -l {} \;
find $dir -name $file -type f -mtime -$retain -exec ls -l {} \;

done < /home/input.txt
 
Just do a set -f at the beginning of the script, and leave it in that mode for the whole time. find will do its own matching of *.log, so you don't need the shell to do it.

Annihilannic.
 
thanks annihilannic - this is perfect. just out of interest (??), how would you put the \ in without using set -f ?

thanks again !!
 
You might have to escape it when you define the variable, i.e. \\*log.

Alternatively you could use "$file" which would prevent the shell doing filename generation on it.

[tt]$ touch a.log
$ touch b.log
$ t="*.log"
$ echo $t
a.log b.log
$ echo "$t"
*.log
$[/tt]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top