-2

Need to delete logs with different name from different locations and need to delete the logs more than 5 days.

Ex:

/abc/bcd/fgh/log/log1.txt
/abc/bcd/fgh/test/log2.log
/test/urc/mhg/event.log
/hjy/jghd/qwer/nbcvd/eda.log
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
palani
  • 1
  • Do you need to do this as a one time thing or as a regularly occurring event? – moneyt Dec 08 '17 at 16:51
  • I need to schedule this script in to delete logs daily. – palani Dec 08 '17 at 16:53
  • Have you done any research on this yet? Do you have any code that you've already tried? This site is designed to help answer specific questions, not to blindly do work for people. – Jason Rush Dec 08 '17 at 17:11
  • "Delete log more than 5 days"? Older than? Is there something that automatically creates a new log file each day, or what is it exactly? – ilkkachu Dec 08 '17 at 17:40
  • Hi ilkkachu, Need to delete older than 5 days and new log files are creating automatically. I have tried the below and it is working, but I am not sure how to implement this for different directories. find /server/admin/ -type f -name 'index*' -mtime +1 -exec rm -f {} ; – palani Dec 08 '17 at 17:56
  • @palani. Unless the programs are restarted, you can't just delete the files. Else all of the old contents will remain and will be rewritten to the disk whence the program writes a log entry. – Shōgun8 Dec 08 '17 at 18:45

2 Answers2

2

You should take a look at logrotate which is designed to automate this sort of thing for you. You'll make a config file telling it where the logs are and how you want them handled and it will schedule and rotate/remove them for you.

moneyt
  • 131
  • Hi Moneyt, I am not having access to root and I cant able to login as root user. I need to create script and need delete logs. I think logrotate will work in this case ? – palani Dec 08 '17 at 17:12
0

add this script to crontab

 #!/bin/bash

LogArray=()
LogArray+=('/abc/bcd/fgh/log/log1.txt')
LogArray+=('/abc/bcd/fgh/test/log2.log')
LogArray+=('/test/urc/mhg/event.log')
LogArray+=('/hjy/jghd/qwer/nbcvd/eda.log')

    for (( i=${#LogArray[@]}-1; i>=0; i-- )); do
        if test `find ${LogArray[$i]} -ctime +5`
            then
                truncate -s 0 ${LogArray[$i]}
                chmod ""$(stat -c %a ${LogArray[$i]})"" ${LogArray[$i]}
        fi
    done

    exit 0
Shōgun8
  • 711
  • Hi Octavian, Thank you. If you dont mind will you explain how it work, It will help and I will learn. – palani Dec 08 '17 at 17:15
  • I think you're missing a closing brace there. But why the loop at all if you expand the whole array inside the loop anyway? – ilkkachu Dec 08 '17 at 17:39
  • Sorry, I've updated it. So this script just adds arguments that you can specify (such as file names), into an array. Then it cycles through the array and checks the ctime of the file. If the ctime is older then 5 days, it will truncate the file and update the ctime. if it is not older than 5 days then it will leave the file alone. – Shōgun8 Dec 08 '17 at 17:54
  • @ikkaqchu you're correct. I overlooked that. I updated that too. – Shōgun8 Dec 08 '17 at 18:41