1

I am new to shell scripting. I want to clear log files in a directory based on the dates. I have managed to gather this code:

DIR1='/home/u01/app/oracle/servers/DIR1/logs'
DIR2='home/u01/app/oracle/servers/DIR2/logs'
DIR3='home/u01/app/oracle/servers/DIR3/logs'
Admin='/home/u01/app/oracle/fmw/admin_poc001/domain/domain_IAM_POC01/servers/AdminServer/logs'

echo "Enter the path"
read path
if [ $path == $DIR1 || $path == $DIR2 || $path == DIR3 || $path == $Admin ]
then
  echo  $path "check your path"

find $path -mtime +10 --# This line here "10" represents listing out the last 10 days logs
for i in *.log* *.out*
do
  cat /dev/null >"$i"
done
else
echo "Re-enter the Correct path"
read path
fi

This script gives me output when I give input like this:

Enter your path :
/home/u01/app/oracle/servers/DIR1/logs

It will clear all the log files until the date of running that script.

But I want that if I run this script I can clear the log files for times other than the last 10 days, i.e., if I run this today (24/06/2015), then I can clear the log files before the date 14/06/2015.

Also: rather than entering the full directory path, can I store that in a variable and just type that variable name (instead of entering the full directory path)?

For example, instead of entering /home/u01/app/oracle/servers/DIR1/logs I am looking for a procedure where I can just enter DIR1 so that it can go into that directory without the total path.

1 Answer 1

3

There is a tool called logrotate read the manual page man logrotate it is started by cron and probably already cleaning the log directory you can add your own configurataion files to /etc/logrotate.d

4
  • 1
    probably not configured to do the ones in home directory, but you can run it manually, or add configuration. Jun 24, 2015 at 19:29
  • 1
    Note that logrotate may or may not be available on the concerned OS, which is not stated clearly in OP. Jun 24, 2015 at 19:33
  • Hi thanks for response but can you please elaborate about this, as i am very new to shell scripting , what are the configuration files and i need to clear all *.log files and *.out files based on the date and day which has to clear the log files accordingly.thanks in advance. please share an example script if anyone has
    – mahesh
    Jun 25, 2015 at 20:11
  • have you read the manual page for logrotate yet ? whenyou try something and get stuck, write a new question.
    – X Tian
    Jun 26, 2015 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.