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

Re: Bash script question

On Thu, Dec 07, 2006 at 12:16:54PM -0600, 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.

The easiest way wouldn't involve the filename at all. If you
know that a file created on date D is stamped with date D --
i.e., if your files all look like so:

(13:09) slaniel@whitehail:~$ ls filename-20061207.tar.gz 
-rw-r--r--  1 slaniel slaniel 0 2006-12-07 13:09 filename-20061207.tar.gz

-- then you can just use find(1). You could do something

find directoryName -mtime +X -print0 |xargs -0 rm '{}'

That's untested, but something like that should do the
trick. It finds all files whose modification date is X days
ago or before ("-mtime +X"), then pipes the resulting list
into xargs and deletes them.

If that approach won't work -- because, say, files created
on date D are modified later -- then you'll have to do some
more magic. I'd probably use Perl with the DateManip library
for that. But hopefully that's unnecessary.

Stephen R. Laniel
Cell: +(617) 308-5571
PGP key: http://laniels.org/slaniel.key

Attachment: signature.asc
Description: Digital signature

Reply to: