[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash script question

Nate Bargmann wrote:
I have a directory of files that are created daily using filename-`date +%Y%m%d`.tar.gz so I have a directory with files whose
names advance from filename-20061201.tar.gz to filename-20061202.tar.gz
to filename-20061203.tar.gz and so on.  Based on the date in the
filename, I would like to delete any than are X days older than today's
date.  So, I'm not interested in the actual created/modified date, just
the numeric string in the name.

Despite working in Bash for the past ten years or so, my shell
scripting skills are poor.  Logically, this seems like a simple test of
whether the name is older than the current day - X, but I'm having
trouble putting this into a script.

Here's a function, with an example invocation, that I'm pretty sure does what you want, assuming the file filename-20061203.tar.gz exists:


# give $1=filename $2=days_old
function remove_if_too_old
  TODAY=`date +%Y%m%d`
if [[ `echo "$1" | cut -f 2 -d "-" | cut -f 1 -d "."` < `expr $TODAY - $2` ]]
    rm $1

remove_if_too_old filename-20061203.tar.gz 2


Reply to: