We have an app deployed in a server, which, under some scenarios, generates some logs in the below format.


Now, due to this format, there isn't one log, but several such logs all with same process_name.hostname.common_text.{error/info/warning} part, but with rest of the parts different, due to difference in date and time. But as far as logrotate is concerned, it treats all these as individual logs and it will retain 1 such copy of each log if i say

rotate 1

in logrotate conf.

But as far as I am concerned, these are all same logs and I don't want to retain any log other than the recent 1 log from each category (error/info/warning). How would i go about this?

I thought of writing a script which will run weekly in cron. And this script will check for the recent file from each category (error/info/warning) using timestamp (ls -ltr) and then, delete rest of such logs. But, again, it's getting too complicated if i try to put it in script.

I am looking for something like this.

ls -ltr |grep process_name.hostname.common_text.log.error |head -n -1
ls -ltr |grep process_name.hostname.common_text.log.info |head -n -1
ls -ltr |grep process_name.hostname.common_text.log.warning |head -n -1

The above 3 commands will return the names of all the process_name.hostname.common_text.log.{error/info/warning}.date.time logs except the recent one.

1) Is it possible to pass the output of each of the above commands to exec or xargs in the same line, so that i can run rm -rf against it?

2) what's the better way of doing this? coz, the process_name again have 2 different names, so, i'll have to run 6 commands or maybe more than that.

3) Instead of grepping thrice, once for error, and once for info, and once for warning, is there any way i can grep for all 3 in single line?

1 Answer 1


If you use -1 instead of -l for ls, you get only the filenames and can pass them directly to rm. I would use something like this:

rm $(ls -1tr process_name.hostname.common_text.log.error* | head -n -1) \
$(ls -1tr process_name.hostname.common_text.log.info* | head -n -1) \
$(ls -1tr process_name.hostname.common_text.log.warning* | head -n -1)
  • Thanks for "-1" option in ls command. Usually, I use awk to get the filenames after ls -lrt. Learning of the life :)
    – Makesh
    Jun 10, 2020 at 12:05

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.