boudiccas
boudiccas

Reputation: 337

How to delete dated txt files as part of a bash script under linux

I'm trying to write a script, which is part of a larger script to generate the files, which deletes the dated backups after so many days. They are in the form of test-$(date +"%Y-%m-%d.txt")

This is what I've got, which isn't really working;find ~/cron/obnam -type f -mtime +3 | xargs rm>>$LOG-FILE 2>&1

This is to be used on Debian 7, under Linux.

Upvotes: 1

Views: 144

Answers (2)

Marty McGowan
Marty McGowan

Reputation: 366

for generality, i like functions:

findtest() { find ~/cron/obname -type f -mtime +${1:-3} -name 'test-*.txt'; }

lists the candidates. e.g.

findtest      # default is 3 days
findtest  31  # say for 31 days

then

trimtest () { findtest ${1:-7} | xargs rm -v ; }   # and executed

trimtest >> $LOGFILE

this way separates action ( the finding, ) policy ( remove after N days), and recording ( appending to the log file). depending on your needs, you might also rewrite to pass the whole flag, and allow both older or newer options.

good luck.

Upvotes: 0

janos
janos

Reputation: 124648

Not 100% accurate, but maybe this is good enough for the case that you describe:

find ~/cron/obnam -type f -mtime +3 -name 'test-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1

If you have some corner cases that this does not handle well, please drop a comment and I will amend.

Upvotes: 1

Related Questions