Help in unix scripting

Hi Experts;
I am very new in Unix shell scripting. As part of my job, I have to write a script to delete the log file. Can anybody help me in unix scripting to delete the log file older than 30 days?.

Thanks
Thankbe

Hi.

What log file would that be?

Assuming any log file (i.e. possibly all log files), look at the man page for find(1), specifically the -mtime option.

For a specific log file look at rm(1).

I wrote a script to delete the log file. id ont know whether its right. if anybody can correct this script, i really appreciated.
First the script is asking the condition to delete. if its "Y" then the script will take the count of the file to be deleted. if the count is greater than "0" then it will delete the logs otherwise the script will exit.

#!/bin/ksh
/usr/bin/echo "Do you want to delete the log files older than 30 days"
if [[ $? == y ]]; then
   /usr/bin/echo "Checking the count of the files to be Deleted"
   count=find /var/lmp/log -type f -name "*.log" -a -name "*.act" -mtime +30 -exec ls -l {} \;
   if [[ count -gt 0 ]]; then
 find /var/lmp/log -type f -name "*.log" -a -name "*.act" -mtime +30 -exec rm -f {} \;
   else
 /usr/bin/echo "there is no file to delete"
   fi
else
   /usr/bin/echo "Exiting from the script"
fi

Some modifications:

#!/bin/ksh
printf "Do you want to delete the log files older than 30 days: "
read answer
case "$answer" in
  y|Y) echo "Checking the count of the files to be Deleted"
     count=$(find /path/to/dir -type f -name "*.log" -o -name "*.act" -mtime +30 | wc -l)
     if [ $count -gt 0 ]; then
       find /path/to/dir -type f -name "*.log" -o -name "*.act" -mtime +30 -exec rm -f {} \;
     else
       echo "there is no file to delete"
     fi;;
   *)  echo "Exiting from the script"
esac

Although, this could be simplified.

1 Like