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

Re: Bash script question



On Thu, Dec 07, 2006 at 11:41:23AM -0700, John Schmidt wrote:
> On Thursday 07 December 2006 11:16, Nate Bargmann wrote:
> > Since there is a lot of knowledge on this list, I thought I'd aske
> > here.
> >
> > This may be trivial, but I'm not even sure how to search for what I
> > want to do.
> >
> > 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.
> 
> This will require some debugging on your part, but hopefully this will be 
> pretty straightforward
> 
> old_date=20061201
> for file_name in *tar.gz; 
> 	do
> 		file_to_remove_date=echo $file_name | tr -d [:alpha:] | tr -d [=-=];
> 		if test $file_to_remove_date -le $old_date; 
> 			then
> 				echo "rm $file_name";
> 		fi
> 	done
> 
> 
> There are certainly better ways to do this.  You can do a man tr to see what 
> the above is doing.  
> 
Hi John and Nate,
here is a gem to address the 'x days older than today's date'
DAYS_OLDER=5
old_date=$(date --date="now - $DAYS_OLDER day" +%Y%m%d)
cheers,
--Kev

-- 
|  .''`.  == Debian GNU/Linux == |       my web site:       |
| : :' :      The  Universal     | debian.home.pipeline.com |
| `. `'      Operating System    | go to counter.li.org and |
|   `-    http://www.debian.org/ |    be counted! #238656   |
|     my keysever: pgp.mit.edu   |     my NPO: cfsg.org     |

Attachment: signature.asc
Description: Digital signature


Reply to: