From owner-freebsd-questions@FreeBSD.ORG Wed Oct 24 23:58:35 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id ADFD616A417 for ; Wed, 24 Oct 2007 23:58:35 +0000 (UTC) (envelope-from SRS0=+OB1HM=PS=shell.siscom.net=vogelke@siscom.net) Received: from lamorack.siscom.net (lamorack.siscom.net [209.251.2.116]) by mx1.freebsd.org (Postfix) with ESMTP id 6F93013C480 for ; Wed, 24 Oct 2007 23:58:35 +0000 (UTC) (envelope-from SRS0=+OB1HM=PS=shell.siscom.net=vogelke@siscom.net) Received: from shell.siscom.net ([209.251.2.80]) by lamorack.siscom.net with esmtp (Exim 4.62) (envelope-from ) id 1IkpiO-0004FM-0H; Wed, 24 Oct 2007 19:32:44 -0400 Received: by shell.siscom.net (Postfix, from userid 2198) id D340C115529; Wed, 24 Oct 2007 19:32:43 -0400 (EDT) Date: 24 Oct 2007 19:32:18 -0400 Message-ID: <20071024233218.91214.qmail@kev.nowhere.usa> From: "Karl Vogel" To: andrew clarke In-reply-to: <20071024124508.GA17482@ozzmosis.com> (message from andrew clarke on Wed, 24 Oct 2007 22:45:08 +1000) Organization: Sumaria Systems Inc. X-Disclaimer: I don't speak for the USAF or Sumaria. X-GPG-ID: 1024D/711752A0 2006-06-27 Karl Vogel X-GPG-Fingerprint: 56EB 6DBF 4224 C953 F417 CC99 4C7C 7D46 7117 52A0 Cc: freebsd-questions@freebsd.org Subject: Re: rename file based on file's timestamp X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: vogelke+software@pobox.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 24 Oct 2007 23:58:35 -0000 >> On Wed, 24 Oct 2007 22:45:08 +1000, >> andrew clarke said: A> I have a series of files in a directory: A> -rw-r--r-- 1 ozzmosis ozzmosis 115201253 Jul 28 2006 209.mp3 A> -rw-r--r-- 1 ozzmosis ozzmosis 115201253 Jul 31 2006 212.mp3 A> Now I want to rename these so the new filenames are based on the file's A> timestamp, like so: A> -rw-r--r-- 1 ozzmosis ozzmosis 115201253 Jul 28 2006 2006-07-28.mp3 A> -rw-r--r-- 1 ozzmosis ozzmosis 115201253 Jul 31 2006 2006-07-31.mp3 A> I can write some Python code to do this, but maybe there is another way, A> perhaps using a shell script. Any thoughts? The script below is in Perl, but converting it to Python probably wouldn't be too difficult. -- Karl Vogel I don't speak for the USAF or my company Loch Ness monster surfaces in Jersey bathtub --Weekly World News headline --------------------------------------------------------------------------- #!/usr/bin/perl -w # Rename each regular file in the argument list using its modtime. # Add the inode number if that's not enough to create a unique file. use strict; foreach my $file (@ARGV) { # sanity checks. next unless -f $file; my ($inode, $mtime) = (stat(_))[1, 9]; die "$file: no mtime found\n" unless defined($mtime); # any file extension? my $ext = ''; $ext = "$1" if $file =~ m/(\.\w*)$/; # try date.extension, date.inode.extension, then give up. my ($day, $mon, $year) = (localtime($mtime))[3, 4, 5]; my $date = sprintf("%4.4d-%2.2d-%2.2d", $year + 1900, $mon + 1, $day); my $newfile = $date . $ext; if (-f $newfile) { $newfile = $date . '.' . $inode . $ext; } if (-f $newfile) { warn "unable to rename $file\n"; } else { unless (rename($file, $newfile)) { warn "rename $file to $newfile failed: $!\n"; } } } exit(0);