[LINUX] Manage logrotate generations with a shell script.

If you want to keep 3 generations, you can use the script one liner below. (The idea is to leave 3 generations → read as deleting all files other than the 3 generations you want to keep)

(When erasing from the oldest one in time) ls -1rt | head -n -3 | xargs rm -rf

(When deleting the oldest date such as testlog20200701) ls -1 | head -n -3 | xargs rm -rf


Description (when erasing the old one judging from the character string in the log)

  1. Place a dummy log

touch testlog2020070{1..9}

  1. Confirmation

ls -la

-rw-r--r--. 1 root root 0 May 17 16:40 testlog20200701 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200702 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200703 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200704 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200705 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200706 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200707 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200708 -rw-r--r--. 1 root root 0 May 17 16:40 testlog20200709

  1. Extraction of deletion target I want to keep 20200707 to 20200709 → (read as I want to delete testlog 20200701 to 20200706)

ls -1 | head -n -3

testlog20200701 testlog20200702 testlog20200703 testlog20200704 testlog20200705 testlog20200706

  1. Delete the extracted log

ls -1 | head -n -3 | xargs rm -rf

Verification

ls -la

testlog20200707 testlog20200708 testlog20200709

I was able to leave the desired 3 generations.

*) (When erasing from the oldest one in time) is realized by performing with ls -1rt. Option t Sort by time Option r Sort in reverse


It is assumed that the log rotation itself is performed using the Linux standard logrotate.d, and only the deleted part is performed by the shell script. (Because the deletion script of logrotate.d is processed in the form of x days ago, if the deletion script does not work for some reason, the log may remain)

Recommended Posts

Manage logrotate generations with a shell script.
View today's weather forecast with a shell script
Try programming with a shell!
[Linux] Copy data from Linux to Windows with a shell script
Process the files in the folder in order with a shell script
[Linux] Write a deployment tool using rsync with a shell script
Automate environment construction with Shell Script
Write a batch script with Python3.5 ~
Process the contents of the file in order with a shell script
[Ubuntu] How to execute a shell script
Create a star system with Blender 2.80 script
Creating a shell script to write a diary
Transfer network device logs to syslog server and manage generations with logrotate
Shell script basics # 2
Erase & generate serial number files with shell script
Launch a shell while a Python script is running
Try creating a FizzBuzz problem with a shell program
A shell script that puts Webmin into Alpine Linux
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Tips for opening a scene with a broken reference with a script
A4 size with python-pptx
Shell script command replacement
Scraping with scrapy shell
Shell script special variables
Shell script @ study memo
Decorate with a decorator
A shell script that numbers duplicate names when creating files
I replaced the Windows PowerShell cookbook with a python script.
[Django] Manage settings like writing in settings.py with a model
How to run a Python program from within a shell script
[Python, shell script, team development] Create a nifty Git repository
Let's create a script that registers with Ideone.com in Python.
Access the file with a relative path from the execution script.
Behavior when giving a list with shell = True in subprocess
Upload images taken with an action camera with a Mapillay script
I'll never forget how to write a shell script, don't forget! !!