Operating System - HP-UX
1837197 Members
2231 Online
110115 Solutions
New Discussion

how to auot delete a file

 
lb_z
Occasional Contributor

how to auot delete a file

hi,room
how to monitor a file,when it reach the size defined,the script can delete if automaticaly?
thanks
3014405
11 REPLIES 11
Peter Kloetgen
Esteemed Contributor

Re: how to auot delete a file

Hi,

you can use the find- command for this:

find -name file_name -a -size 10000 -exec rm {}\;

--> looks for given file name, and checks if given file size is matched, then automatically removes the file.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Pete Randall
Outstanding Contributor

Re: how to auot delete a file

I'm sure there are more gifted scripters who will respond with more elegant solutions. But, you could just cron a find command to run every 10 minutes or so that would search for name and size and delete if found. Something like:
find /pathname \( -name filename -size +filesize \) -exec rm {} \;

HTH,
Pete

Pete
Steven Sim Kok Leong
Honored Contributor

Re: how to auot delete a file

Hi,

One way is to use stat in your cron script:

#!/sbin/sh

file=/tmp/abc

if [ "`stat $file | head -2 | tail -1 | awk '{print $2}'`" -gt "65535" ]
then
rm -f $file
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Peter Kloetgen
Esteemed Contributor

Re: how to auot delete a file

Hi again,

sorry, typo in my first answer:

forgot to give where to search...

find /path_to_directory ...........

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Mark van Hassel
Respected Contributor

Re: how to auot delete a file

or:

file=[your file]
lim=[file size limit]

if [ $(ls -l $file | awk '{ print $5)' -gt $lim ]
then
rm $file
fi
The surest sign that life exists elsewhere in the universe is that none of it has tried to contact us
Steven Sim Kok Leong
Honored Contributor

Re: how to auot delete a file

Hi,

find is indeed the most elegant way. Just be aware that by default, the number after -size is in 512-byte blocks by default.

If you want it to be in bytes, it must be followed by the 'c' character e.g. delete /tmp/my_file if exits 1024 bytes:

# find /tmp -name my_file -size +1024c -exec rm -f {} \;

Hope this helps. Regards.

Steven Sim Kok Leong
lb_z
Occasional Contributor

Re: how to auot delete a file

thanks anyone
and i am sorry i didn't desc it clearly,i actually want to dete the size when its size exess the x%,as you know,some log file increase rapadily. the x is my define

thans a lot
3014405
Peter Kloetgen
Esteemed Contributor

Re: how to auot delete a file

Hi,

it's not a good idea to remove logfiles, it's better to trim them to a defined size. You can use SAM for this, go to routine tasks, and trim your file to the desired size. Of course you can do this with a script also:

find / -name $1 -a -size +max_size -exec ls {} \; | tail -n 500 > my_file_trimmed

cat my_file_trimmed > $1

rm my_file_trimmed

This would get the last 500 lines of your logfile (which are the newest entries of course), put them into a dummy file and after this the original file is overwritten with the contents of the dummy file, which is deleted at the end. Call the script with the file, which you want to trim as argument, for example, your script name is trimmer.sh, and you want to trim a file named log_file, do the following command to get it to work:

trimmer.sh log_file

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Daimian Woznick
Trusted Contributor

Re: how to auot delete a file

If there are several log files you want to monitor I would do the following:

First create a file with the file name and the maximum log size in it:

/var/adm/syslog/syslog.log 30000
/var/adm/cron/log 5000

Separate the file name and maximum size with a space. Let's call this file log.file.parameters.

Now we would create a script that could be scheduled in cron.

#!/bin/ksh

for log_file in `awk '{print $1}' log.file.parameters`
do

# Get the max size
max_size=`grep ^"$log_file " log.file.parameters | awk '{print $2}'`

# Check the size of the file
file_size=`ls -al $log_file | awk '{print $5}'`

# Compare the size values
if [ "$file_size" -ge "$max_size" ]
then

# Copy the file to a backup, compress the backup to conserve space, and null the data in the orig file
cp $log_file $log_file.BAK
compress $log_file.BAK
cat /dev/null > $log_file

fi

done

Hope this helps.
Bill Thorsteinson
Honored Contributor

Re: how to auot delete a file

The script logrotate will rotate log files on a periodic
basis. You can optionally,
comress the old files.
Rotation criteria can be size or time period.

If necessary, you can script in commands to restart or
interrupt programs that keep their log files open.

It is on the porting site.
Ramakrishna_4
New Member

Re: how to auot delete a file

Hi
My question is almost the same, but I will have to monitor 4 directories and if any file in those 4 dir exceeds 'X'MB(X is a var that the script reads from a config file and then accordingly deletes the files based on that size) , then it needs to be deleted.

i am a little confused with the usage of 'awk' and 'find'.

Please suggest.