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:


echo "Enter the path"
read path
if [ $path == $DIR1 || $path == $DIR2 || $path == DIR3 || $path == $Admin ]
  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*
  cat /dev/null >"$i"
echo "Re-enter the Correct path"
read path

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

Enter your path :

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


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

  • 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.