From owner-freebsd-questions@FreeBSD.ORG Thu Feb 14 00:01:32 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 559FC93C for ; Thu, 14 Feb 2013 00:01:32 +0000 (UTC) (envelope-from vogelke@hcst.net) Received: from beta.hcst.com (beta.hcst.com [192.52.183.241]) by mx1.freebsd.org (Postfix) with ESMTP id 0AECE912 for ; Thu, 14 Feb 2013 00:01:31 +0000 (UTC) Received: from beta.hcst.com (localhost [127.0.0.1]) by beta.hcst.com (8.14.3/8.14.3/Debian-9.4) with ESMTP id r1E01PXx024242 for ; Wed, 13 Feb 2013 19:01:25 -0500 Received: (from vogelke@localhost) by beta.hcst.com (8.14.3/8.14.3/Submit) id r1E01PFO024241; Wed, 13 Feb 2013 19:01:25 -0500 Received: by kev.msw.wpafb.af.mil (Postfix, from userid 32768) id 32A87BF5D; Wed, 13 Feb 2013 19:00:27 -0500 (EST) To: freebsd-questions@freebsd.org In-reply-to: <511BE12C.204@tundraware.com> (message from Tim Daneliuk on Wed, 13 Feb 2013 12:53:32 -0600) Subject: Re: Fun Scripting Problem Organization: Array Infotech X-Disclaimer: I don't speak for the USAF or Array Infotech. X-GPG-ID: 1024D/711752A0 2006-06-27 Karl Vogel X-GPG-Fingerprint: 56EB 6DBF 4224 C953 F417 CC99 4C7C 7D46 7117 52A0 References: <511BDB13.3060005@tundraware.com> <13CA24D6AB415D428143D44749F57D7201EA7EBB@ltcfiswmsgmb21> <511BE12C.204@tundraware.com> Message-Id: <20130214000027.32A87BF5D@kev.msw.wpafb.af.mil> Date: Wed, 13 Feb 2013 19:00:27 -0500 (EST) From: vogelke+unix@pobox.com (Karl Vogel) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: vogelke+unix@pobox.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Feb 2013 00:01:32 -0000 >> On Wed, 13 Feb 2013 12:53:32 -0600, >> Tim Daneliuk said: T> The only way to determine the date of the file is by looking at its stat T> info. There is nothing the file name or content that could be used to T> infer this. Being a pedantic twit, I interpreted "stat info" to mean "info you can get from something that calls stat()". The script below runs on BSD, Linux, or Solaris if you have GNU find installed. Season to taste. -- Karl Vogel I don't speak for the USAF or my company Mom was so overprotective, she only let us play "Rock, Paper". --------------------------------------------------------------------------- #!/bin/ksh # usage: keepfiles DIR export PATH=/usr/local/bin:/bin:/usr/bin tag=${0##*/} here=$(pwd) tmp="$here/keep$$" # Sanity check. die () { echo "$tag: FATAL: $@" >&2; exit 1; } # Don't repeat yourself. Use undocumented option %Ts to # avoid dealing with fractional seconds. listdir () { find . -type f -printf "%TY%Tm%Td %Ts|%p\n"; } case "$#" in 0) die "need a directory" ;; *) work="$1" ;; esac # List files by date generated. mkdir $tmp || die "$tmp: mkdir failed" test -d $work || die "$work: not a directory" cd $work || die "cd $work failed" months=$(listdir | cut -c1-6 | sort -u) for mon in $months do listdir | # Get the files to check, grep "^$mon" | # ... look for each month, sort -n | # ... sort by modtime, cut -d'|' -f2 | # ... get the path, tail -1 | # ... last one's oldest, sed -e 's!^\(.*\)!mv \1 '$tmp'!' | # ... write a mv command, sh # ... and run it. done # Clean up. cd $here echo "Keep the stuff in $tmp" exit 0