Showing results for 
Search instead for 
Did you mean: 

Deleting old files (logs or other files)

Go to solution
Pankaj Mandalia
Occasional Contributor

Deleting old files (logs or other files)

I have used the following command to delete files:
find /psft/pt8.42/dev/appserv/prcs/DEV_2/log_output -name '*CUPOFOCB*' -type d -mtime +5 -exec rm {} \;

However this removes files that are 5 days or older. I have a process that generates a log file every minute. I am trying to use the integer in a decimal form like -mtime +0.5 but it does not work.
Can you please help me device a script that will do this for the fraction of a day?
Thanks in advance.
-Pankaj Mandalia
A. Clay Stephenson
Acclaimed Contributor

Re: Deleting old files (logs or other files)

All the -mtime option understands is integers. What you can do is use the touch command to create a reference file with a timestamp of your choice and then use find's -newer myreffile option rather than -mtime. Man touch and find for details. I also note that you are using -type d but I suspect you want -type f to delete regular files. It's always a good idea to replace your -exec rm {} \; with something like -exec ls -l ${} \; until you know you have the find's filter exactly right.
If it ain't broke, I can fix that.
Pankaj Mandalia
Occasional Contributor

Re: Deleting old files (logs or other files)

Hi Clay:
THank you for the suggestion and reply... The reason I used d was for a directory and I tried rmdir {} also but that did not make any difference. I will try the -newer approach and let you know the results.
Thank you.
Tor-Arne Nostdal
Trusted Contributor

Re: Deleting old files (logs or other files)

Hi Pankaj
Here's a small example of using a reference file:

touch -t YYYYMMDDhhmm /tmp/ref_file

myhost# date
Fri Dec 10 11:39:54 MET 2004

myhost# pwd

myhost# ll tor*
-rw------- 1 root sys 6562 Nov 2 18:05 tor
-rw-r--r-- 1 root sys 0 Dec 10 11:37 tor2

myhost# touch -t 200412010000 ref_file
myhost# ll ref_file
-rw-r--r-- 1 root sys 0 Dec 1 00:00 ref_file

myhost# find /tmp ! -newer /tmp/ref_file -type f -name "tor*" -exec ll {} \;
-rw------- 1 root sys 6562 Nov 2 18:05 /tmp/tor

In the example you can see that I have two files that matches the name specification. One that is older than the reference file.
Since -newer normally would seelct the newest file - I must negate (use ! ) to get the oldest one.
It is also important to stack the find options in proper order. Therefore I first look for old files, then search for proper type (ordinary file), then a matching filename.

Removing directories:
Your rmdir command probably failed because there where files in the directory.
If you would delete an entire directory structure which contain files you should use the command:
find "your options" -type d -exec rm -r {} \;
rm -r will recursively remove all files + directory

Be careful with this and do as Clay tells - test it first some times with an ls command.

Best regards
I'm trying to become President of the state I'm in...
Kari Pannila
Frequent Advisor

Re: Deleting old files (logs or other files)

I have used perl for this.
Here is an example how to send ITO alarm
if file is older than n seconds:

## Perl script that tells the "age" of the file in seconds

$FILE = $ARGV[0]; ##Filename
$DIFF = $ARGV[1]; ##Accepted time (seconds) of last modification
$TODAY = time();
$WRITE_SECS = (stat($FILE))[9];
$AGE_MIN = $AGE/60;
if ( $AGE > $DIFF ) {

printf "Alarm! %s is %d min old\n",scalar $FILE, $AGE_MIN;
`$ENV{OPCMSG} sev=warning appl=OS msg_grp=OpC object=opcmsg msg_text="Check fil
e $FILE. Last change $AGE_MIN minutes ago"`;
Kari Pannila