From owner-freebsd-questions@FreeBSD.ORG Mon Jan 4 18:16:17 2010 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 DE3DE10656A5 for ; Mon, 4 Jan 2010 18:16:17 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) by mx1.freebsd.org (Postfix) with ESMTP id 9CF8B8FC1F for ; Mon, 4 Jan 2010 18:16:17 +0000 (UTC) Received: from r55.edvax.de (port-92-195-53-52.dynamic.qsc.de [92.195.53.52]) by mx01.qsc.de (Postfix) with ESMTP id EF5A13DA18; Mon, 4 Jan 2010 19:16:14 +0100 (CET) Received: from r55.edvax.de (localhost [127.0.0.1]) by r55.edvax.de (8.14.2/8.14.2) with SMTP id o04IGAkW001994; Mon, 4 Jan 2010 19:16:10 +0100 (CET) (envelope-from freebsd@edvax.de) Date: Mon, 4 Jan 2010 19:16:10 +0100 From: Polytropon To: =?ISO-8859-1?Q?D=E1rio?= "P." Message-Id: <20100104191610.5054255f.freebsd@edvax.de> In-Reply-To: <1262624558.9656.16.camel@dasp-laptop> References: <1262624558.9656.16.camel@dasp-laptop> Organization: EDVAX X-Mailer: Sylpheed 2.4.7 (GTK+ 2.12.1; i386-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-questions@freebsd.org Subject: Re: Rename pictures in the command-line interface X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Jan 2010 18:16:18 -0000 On Mon, 04 Jan 2010 18:02:38 +0100, D=E1rio "P." wrote: > Hello, >=20 > I have one directory with some pictures that I wanna rename (I use csh, > don't know if that matters). >=20 > For exemple, I have: >=20 > b.jpg > bs.jpg > bsd.jpg >=20 > And I wanna change to: >=20 > bsd1.jpg > bsd2.jpg > bsd3.jpg >=20 > I really appreciate if someone can help me. :) I know it's quite ugly and complicatedly written, but maybe the attached script will help. It "just works", but the more I look at it, the more I wish I hadn't written it, or just used sh and its printf %03d mechanism. :-) Keep in mind that the script follows the csh's sorting order to resolve *, which usually is lexicographical order. For example 97.jpg 98.jpg 99.jpg 100.jpg will, after issuing renumber bla jpg result in bla_01.jpg =3D 100.jpg bla_02.jpg =3D 97.jpg bla_03.jpg =3D 98.jpg bla_04.jpg =3D 99.jpg So if you wish to do some file preparation, know that the powerful Midnight Commander can do this for you (select and PF6). Here's the script now. Put it in ~/bin (and add this directory to your $PATH) as "renumber" (or any name you like), give it +x permissions and "rehash" to make it available to the C shell. Then, use "renumber ". It will process ALL files in the current directory (as I said: ugly as sin). #!/bin/csh if ( $1 =3D=3D "" || $2 =3D=3D "" ) then echo "Usage: renumber " echo " Target form: _nn[n]." echo " For 1 to 99 files: nn; for more than 99 files: nnn" exit 1 endif set n =3D `ls -l | wc | awk '{print $1}'` set num =3D `expr $n - 1` echo "${num} files to handle." set base =3D $1 set extn =3D $2 set n =3D 0 foreach f ( *.${extn} ) set n =3D `expr $n + 1` if ( ${num} > 99 ) then if ( ${%n} =3D=3D 1 ) then mv "${f}" "${base}_00${n}.${extn}" else if ( ${%n} =3D=3D 2 ) then mv "${f}" "${base}_0${n}.${extn}" else mv "${f}" "${base}_${n}.${extn}" endif else if ( ${%n} =3D=3D 1 ) then mv "${f}" "${base}_0${n}.${extn}" else mv "${f}" "${base}_${n}.${extn}" endif endif end --=20 Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...