0

My sip server generates 3 log files for each hour:

Progress-23May2017-10-00hrs.log Call-23May2017-10-00hrs.log Error-23May2017-10-00hrs.log

So I need to delete all other log files except the current hour log. I tried with this command

 find . -type f -name "*-23May2017-10-00hrs.log" -print

it printed my current time's three log files.

so when I tried this command to verify files not matching the current time log.

find . -type f -name "!(*-23May2017-10-30hrs.log)" -print

it gives me nothing.

How to get my other log files deleted?

3
  • What SIP server is it? Doesn't it implement some sort of log rotation to do this for you? Commented May 23, 2017 at 6:19
  • Use logrotate for that job. Commented May 23, 2017 at 6:44
  • @Thomas it generates log for each hour but i use this sip server for my development purposes thats the reason Commented May 23, 2017 at 6:55

2 Answers 2

0
find /home/fi -maxdepth 1 -iname log\* -type f -mmin +1800 | sort | head -n -1 | xargs rm
1
  • Some explanation would be nice. Commented May 23, 2017 at 11:42
0

First of all: your find processes are not escaped, so that the wildcards * are transformed. To prevent that you can either use backslashes \ or use single-quotes '

So: find . -type f -name '*-23May2017-10-00hrs.log' -print should work fine.

Back to your real questions: There are several ways to solve that. First you can use find's -mtime feature to just listen every file which last modification is x minutes ago. Let's assume your sip server logs at /var/log/sip/

find /var/log/sip/ -type f -name '*.log' -mmin +$((24*60)) -print will show you every file ending with .log and which last modification was 24*60 minutes (1 day) ago.

However you can also use a tool which is made for logging handling and thats logrotate.

logrotate is available on every major unix distribution and allows you to compress files at a certain age, remove them at another ago and so on. The feature list is quite long. It's easy to install, runs as a daemon and you don't have to fiddle around with extra cronjobs.

My suggestions is to use logrotate. Check out it's simplicity at: https://linux.die.net/man/8/logrotate

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.