0

The folder gets populated some jobs that run daily, weekly or monthly. For eg. daily job outputs be like:

daily-2017-16-08.txt
daily-2017-15-08.txt

Weekly be like:

weekly-2017-01-08.txt
weekly-2017-08-08.txt

and monthly is like:

monthly-2017-01-08.txt
monthly-2017-01-07.txt
Yogesh
  • 1
  • 1

1 Answers1

0

Something like this?

#!/bin/bash
for FILE_TYPE in daily weekly monthly 
do
     COUNTER=0
     for FILE in $(ls -t ${FILE_TYPE}-*)
     do
        let COUNTER++
        [ ${COUNTER} -gt 30 ] && rm ${FILE}
     done
done
Zachary Brady
  • 4,200
  • 2
  • 17
  • 40
  • see: [Why *not* parse `ls`?](http://unix.stackexchange.com/q/128985/74329) – Cyrus Aug 16 '16 at 19:25
  • Updated to not parse ls output as recommended by @Cyrus – Zachary Brady Aug 16 '16 at 19:32
  • Is there a way to modify your code so that it automatically takes different file types, without me specifying it? – Yogesh Aug 17 '16 at 19:33
  • I'm not sure I totally understand what you mean by "automatically takes" but you could change the `for` line to look like `for FILE_TYPE in $@` and then you call the shell script like `cleanupProgram.sh daily weekly monthly` in cron and if you ever wanted to add or remove file types you just modify the cron entry. – Zachary Brady Aug 17 '16 at 19:46