From owner-freebsd-questions@FreeBSD.ORG Fri Feb 15 17:03:42 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 40F58BE6 for ; Fri, 15 Feb 2013 17:03:42 +0000 (UTC) (envelope-from gkeramidas@gmail.com) Received: from mail-ee0-f42.google.com (mail-ee0-f42.google.com [74.125.83.42]) by mx1.freebsd.org (Postfix) with ESMTP id CC0EF926 for ; Fri, 15 Feb 2013 17:03:41 +0000 (UTC) Received: by mail-ee0-f42.google.com with SMTP id b47so1882936eek.29 for ; Fri, 15 Feb 2013 09:03:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to; bh=Yq5yEQZumUAK2GSD0MRIlQwr55CYKjDGv2O1vLhpbXw=; b=BswQMKlgTxicpWuCPz4S2qpIeWypMyLrb6THKdkZBa6t5FENyCntyOP6XiGx5KEicw LUhfgq12Ve8lXrs5tRiTfkoXx3+alHiDAZDHe3qGeem/5jXSfBPY/KytCYL75Z80y+KQ iVO/D7CElMPd9tBquSIEIhyirSb2fe1z8EHGIgUa/gstKsT9b85A4d1KSq1oyLKxrgzX eyhvhgzTN2tUPOXlNIvQCx/g0ak6Q3HM0+Ls9lVL1kD8tE0p1X++0+9ATPaxlkGonzBN 8/TJGrR2O4IK9zjqmh4o4VeMYij46y4QEhTmYKAINkVRCr3QN2p0UzQVutAoqEsKruFt Ze+A== X-Received: by 10.14.220.135 with SMTP id o7mr10477380eep.3.1360947814838; Fri, 15 Feb 2013 09:03:34 -0800 (PST) Received: from saturn (217-162-217-29.dynamic.hispeed.ch. [217.162.217.29]) by mx.google.com with ESMTPS id 44sm84513369eek.5.2013.02.15.09.03.33 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 15 Feb 2013 09:03:33 -0800 (PST) Sender: Giorgos Keramidas Date: Fri, 15 Feb 2013 18:03:30 +0100 From: Giorgos Keramidas To: Tim Daneliuk Subject: Re: Fun Scripting Problem Message-ID: <20130215170329.GA27196@saturn> References: <511BDB13.3060005@tundraware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <511BDB13.3060005@tundraware.com> Cc: FreeBSD Mailing List 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: Fri, 15 Feb 2013 17:03:42 -0000 On 2013-02-13 12:27, Tim Daneliuk wrote: > 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? You can try using stat/date to print the month in which each file was modified, e.g: freefall:/home/keramida$ stat -f '%m %N' prs.tar.bz2 1359910210 prs.tar.bz2 freefall:/home/keramida$ date -f '%s' -j '1359910210' '+%Y-%m' 2013-02 Having the mtime of the file in seconds since the epoch (1359910210) should be easy to sort through, e.g. you can find the last modification time with a 'sort -n | tail -1' pipe: freefall:/home/keramida/w/doc-head/el_GR.ISO8859-7/share$ touch xml/glossary.ent freefall:/home/keramida/w/doc-head/el_GR.ISO8859-7/share$ find xml -exec stat -f '%m %N' {} + 1360060289 xml 1360060288 xml/navibar.l10n.ent 1360060288 xml/trademarks.ent 1360060288 xml/libcommon.xsl 1360060288 xml/header.l10n.ent => 1360942284 xml/glossary.ent 1360060289 xml/freebsd.dsl 1360060289 xml/teams.ent 1360060289 xml/freebsd.ent 1360060289 xml/l10n.ent 1360060289 xml/mailing-lists.ent 1360060289 xml/newsgroups.ent 1360060289 xml/translators.ent 1360060289 xml/catalog.xml 1360060289 xml/entities.ent 1360060289 xml/catalog 1360060289 xml/urls.ent freefall:/home/keramida/w/doc-head/el_GR.ISO8859-7/share$ find xml -exec stat -f '%m %N' {} + | sort -n | tail -1 1360942284 xml/glossary.ent Then you can convert the epoch-based times to '%Y-%m' timestamps in a loop, e.g.: freefall:/home/keramida/w/doc-head/el_GR.ISO8859-7/share$ find xml \ > -exec stat -f '%m %N' {} + | while read mtime fname ; do > echo ${mtime} $( date -f '%s' -j "${mtime}" '+%Y-%m' ) ${fname} > done 1360060289 2013-02 xml 1360060288 2013-02 xml/navibar.l10n.ent 1360060288 2013-02 xml/trademarks.ent 1360060288 2013-02 xml/libcommon.xsl 1360060288 2013-02 xml/header.l10n.ent 1360942284 2013-02 xml/glossary.ent 1360060289 2013-02 xml/freebsd.dsl 1360060289 2013-02 xml/teams.ent 1360060289 2013-02 xml/freebsd.ent 1360060289 2013-02 xml/l10n.ent 1360060289 2013-02 xml/mailing-lists.ent 1360060289 2013-02 xml/newsgroups.ent 1360060289 2013-02 xml/translators.ent 1360060289 2013-02 xml/catalog.xml 1360060289 2013-02 xml/entities.ent 1360060289 2013-02 xml/catalog 1360060289 2013-02 xml/urls.ent Having the mtime in seconds as the first column is still conducive to sorting / tail: freefall:/home/keramida/w/doc-head/el_GR.ISO8859-7/share$ find xml \ > -exec stat -f '%m %N' {} + | while read mtime fname ; do > echo ${mtime} $( date -f '%s' -j "${mtime}" '+%Y-%m' ) ${fname}; > done | sort -n | tail -1 1360942284 2013-02 xml/glossary.ent >From that point it should be trivial to select all files whose timestamp is smaller than or equal to 1360942284 - one month of seconds.