Article 4X3N9 scripting delete old data automatically

scripting delete old data automatically

by
greta_g
from LinuxQuestions.org on (#4X3N9)
Hello friends,

on my syslog server i have directories 2018, 2019. this directories are not static. it changes like year. in 2018 / 2019 i have following directories:

[root@testbox ~]# ll 2018/
total 0
drwxr-x--- 33 root root 316 Dec 27 14:31 01
drwxr-x--- 33 root root 316 Dec 27 14:31 02
drwxr-x--- 33 root root 316 Dec 27 14:31 03
drwxr-x--- 33 root root 316 Dec 27 14:31 04
drwxr-x--- 33 root root 316 Dec 27 14:31 05
drwxr-x--- 33 root root 316 Dec 27 14:31 06
drwxr-x--- 33 root root 316 Dec 27 14:31 07
drwxr-x--- 33 root root 316 Dec 27 14:31 08
drwxr-x--- 33 root root 316 Dec 27 14:31 09
drwxr-x--- 33 root root 316 Dec 27 14:31 10
drwxr-x--- 33 root root 316 Dec 27 14:31 11
drwxr-x--- 33 root root 316 Dec 27 14:31 12
[root@testbox ~]# ll 2018/01
total 0
drwxr-x--- 2 root root 6 Dec 27 14:31 01
drwxr-x--- 2 root root 6 Dec 27 14:31 02
drwxr-x--- 2 root root 6 Dec 27 14:31 03
drwxr-x--- 2 root root 6 Dec 27 14:31 04
drwxr-x--- 2 root root 6 Dec 27 14:31 05
drwxr-x--- 2 root root 6 Dec 27 14:31 06
drwxr-x--- 2 root root 6 Dec 27 14:31 07
drwxr-x--- 2 root root 6 Dec 27 14:31 08
drwxr-x--- 2 root root 6 Dec 27 14:31 09
drwxr-x--- 2 root root 6 Dec 27 14:31 10
drwxr-x--- 2 root root 6 Dec 27 14:31 11
drwxr-x--- 2 root root 6 Dec 27 14:31 12
drwxr-x--- 2 root root 6 Dec 27 14:31 13
drwxr-x--- 2 root root 6 Dec 27 14:31 14
drwxr-x--- 2 root root 6 Dec 27 14:31 15
drwxr-x--- 2 root root 6 Dec 27 14:31 16
drwxr-x--- 2 root root 6 Dec 27 14:31 17
drwxr-x--- 2 root root 6 Dec 27 14:31 18
drwxr-x--- 2 root root 6 Dec 27 14:31 19
drwxr-x--- 2 root root 6 Dec 27 14:31 20
drwxr-x--- 2 root root 6 Dec 27 14:31 21
drwxr-x--- 2 root root 6 Dec 27 14:31 22
drwxr-x--- 2 root root 6 Dec 27 14:31 23
drwxr-x--- 2 root root 6 Dec 27 14:31 24
drwxr-x--- 2 root root 6 Dec 27 14:31 25
drwxr-x--- 2 root root 6 Dec 27 14:31 26
drwxr-x--- 2 root root 6 Dec 27 14:31 27
drwxr-x--- 2 root root 6 Dec 27 14:31 28
drwxr-x--- 2 root root 6 Dec 27 14:31 29
drwxr-x--- 2 root root 6 Dec 27 14:31 30
drwxr-x--- 2 root root 6 Dec 27 14:31 31

i want to make script, which work once a month and deletes all directories less then current month. my script looks like:

#!/bin/bash
DAY=$(date -d "$D" '+%d')
MONTH=$(date -d "$D" '+%m')
YEAR=$(date -d "$D" '+%Y')
OLDERYEAR=$((YEAR -1))
cd $OLDERYEAR
for i in $(ls $MONTH); do if [ $i < $MONTH ]; then rm -rf $i; fi done

but this script deletes all month in 2018 and it must delete everything less than 12 (current month).

i try - for i in $(ls); do if [ $i < $((MONTH -1)) ]; then rm -rf $i; fi done

it works, but there are error:

./script1: line 9: 11: No such file or directory

any idea? thankslatest?d=yIl2AUoC8zA latest?i=0Zl1nC5aJz0:d6k2lEAqni4:F7zBnMy latest?i=0Zl1nC5aJz0:d6k2lEAqni4:V_sGLiP latest?d=qj6IDK7rITs latest?i=0Zl1nC5aJz0:d6k2lEAqni4:gIN9vFw0Zl1nC5aJz0
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments