Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 04 Jan 2010 14:58:54 -0500
From:      nvidican@envieweb.net
To:        freebsd-questions@freebsd.org
Cc:        fbsd.questions.list@gmail.com
Subject:   Re: Rename pictures in the command-line interface
Message-ID:  <20100104145854.gdhdk5mb288oswgw@www.envieweb.net>
In-Reply-To: <1262624558.9656.16.camel@dasp-laptop>
References:  <1262624558.9656.16.camel@dasp-laptop>

next in thread | previous in thread | raw e-mail | index | archive | help
Quoting "D=C3=A1rio \"P." <fbsd.questions.list@gmail.com>:

> Hello,
>
> I have one directory with some pictures that I wanna rename (I use csh,
> don't know if that matters).
>
> For exemple, I have:
>
> b.jpg
> bs.jpg
> bsd.jpg
>
> And I wanna change to:
>
> bsd1.jpg
> bsd2.jpg
> bsd3.jpg
>
> I really appreciate if someone can help me. :)
>
> Regards,
>
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.or=
g"
>

Dario,

I'm not personally aware of any single commands which allow =20
substitution using a counter like you're asking, or of a decent way to =20
do what you're asking from the shell script either; however, =20
personally I'd write a simple Perl script to do it. The trick being to =20
be able to find the bsd###.jpg where it left off at in a directory so =20
you don't overwrite existing files if repeatability is important.

Here's something quick/dirty to work with, you can build from here, =20
but try copy/pasting the following code into a new Perl script and run =20
it from withing the directory you want to work:

#!/usr/bin/perl -w


use strict;

my @files =3D `ls`; # gets a list of all files in the current dir
# start a counter at zero, then increment it below:
my $cntr=3D0;
# set counter to the largest bsd###.jpg file in this directory:
map { if (/^bsd(\d+)\.jpg/) { $cntr =3D $1 if($1>$cntr); } } =20
grep(/bsd\d+\.jpg/,@files);

print "Left off last time at $cntr, going to start this time at =20
",++$cntr,".\n";

foreach (@files) {
         chomp();
         # skip all files which are already named bsd###.jpg
         # or are not in ending .jpg
         next if ($_ =3D~ /bsd\d+\.jpg/ || $_ !~ /(\.jpg)$/i);

         my $new =3D $_;
         # use a regular expression to substitute the name
         # (note /i =3D=3D case insensative so it will match '.JPG' as well)
         $new =3D~ s/^(.+)\.jpg$/bsd$cntr\.jpg/i;

         print "Renaming $_ to $new\n";
         # un-comment the line below to actually do the rename:
         # rename($_,$new);
         $cntr++;
}

### END OF SCRIPT ###

An example given a directory with files like:

blah.Jpg
bs432.jpg
bsd11.jpg
bsl.jpg
uh-oh.jpG
yourSelf.JPG

Will give you an output like:

Left off last time at 11, going to start this time at 12.
Renaming blah.Jpg to bsd12.jpg
Renaming bs432.jpg to bsd13.jpg
Renaming bsl.jpg to bsd14.jpg
Renaming uh-oh.jpG to bsd15.jpg
Renaming youSelf.JPG to bsd16.jpg


My $0.02 ... like anything, sure you could do this 100 different other =20
ways, and sure it's not going to be really efficient for large =20
volumes, but in a pinch it'll work pretty reliably.

--
Nathan Vidican
nathan@vidican.com





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100104145854.gdhdk5mb288oswgw>