From owner-freebsd-questions@FreeBSD.ORG Thu Feb 14 05:42:49 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 0E45C78 for ; Thu, 14 Feb 2013 05:42:49 +0000 (UTC) (envelope-from tundra@tundraware.com) Received: from ozzie.tundraware.com (ozzie.tundraware.com [75.145.138.73]) by mx1.freebsd.org (Postfix) with ESMTP id 97A1E762 for ; Thu, 14 Feb 2013 05:42:48 +0000 (UTC) Received: from [192.168.0.2] (viper.tundraware.com [192.168.0.2]) (authenticated bits=0) by ozzie.tundraware.com (8.14.6/8.14.6) with ESMTP id r1E5gYg9077724 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Wed, 13 Feb 2013 23:42:34 -0600 (CST) (envelope-from tundra@tundraware.com) Message-ID: <511C794A.10001@tundraware.com> Date: Wed, 13 Feb 2013 23:42:34 -0600 From: Tim Daneliuk User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: Robert Bonomi Subject: Re: Fun Scripting Problem References: <201302132113.r1DLD6DX003167@mail.r-bonomi.com> In-Reply-To: <201302132113.r1DLD6DX003167@mail.r-bonomi.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (ozzie.tundraware.com [192.168.0.1]); Wed, 13 Feb 2013 23:42:34 -0600 (CST) X-TundraWare-MailScanner-Information: Please contact the ISP for more information X-TundraWare-MailScanner-ID: r1E5gYg9077724 X-TundraWare-MailScanner: Found to be clean X-TundraWare-MailScanner-From: tundra@tundraware.com X-Spam-Status: No Cc: freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Feb 2013 05:42:49 -0000 On 02/13/2013 03:13 PM, Robert Bonomi wrote: > >> Date: Wed, 13 Feb 2013 12:27:31 -0600 >> From: Tim Daneliuk >> Subject: Fun Scripting Problem >> >> I know how to do this in Python, but I really want to do it in >> straight Bourne shell. I have some ideas, but I thought I'd >> give you folks a crack at this Big Fun: >> >> a) You have a directory of files - say they're logs - generated >> at nondeterministic intervals. You may get more than one a day, >> more than one a month, none, or hundreds. >> >> b) To conserve space, you want to keep the last file generated >> in any given month (the archive goes back for an unspecified >> number of years), and delete all the files generated prior to >> that last file in that same month. >> >> c) Bonus points if the problem is solved generally for either files >> or directories generated as described above. >> >> These are not actually logs, and no, I don't think logrotate can >> do this ... or can it? > > here's a one-liner: > rm ` \ > stat -f "%SB %B %N" * \ > | sort -k5nr \ > | cut -c1-7,17-20,32- \ > | awk 'BEGIN {a="";b=0;c=0} $1==a && $2==b && $3=c {print $4;}{a=$1;b=$2;c=$3}' \ > ` > > This selects on creation date. change the B (both of them) in the stat > call to use a different timestamp Thanks to all that took the time. Interesting responses. It will be fun to cook up my own version. -- ---------------------------------------------------------------------------- Tim Daneliuk tundra@tundraware.com PGP Key: http://www.tundraware.com/PGP/