[PLUG] Rename script

Sandy Herring sandy at herring.org
Fri Apr 26 00:37:27 UTC 2002


On Thu, 25 Apr 2002, Matt Alexander wrote: 
> On Thu, 25 Apr 2002, Don Buchholz wrote:
> 
> > Michael Montagne wrote:
> > >
> > > I need a shell script to rename many files.  The files names look like:
> > > "N Clackamas Conformance Vol 2 01-18-00_S607.tif" and
> > > "N Clackamas Conformance Vol 2 01-18-00_S608.tif".
> > > I want to strip the prefix and just have S607.tif and S608.tif.
> > > I think the prefixes are all the same.
> > > For sure they all have an underscore preceding the part that I want.
> > > The easy part is that they are all in the same directory.
> > >
> >
> > #!/bin/sh
> > for f in *
> > do
> > 	newf=`echo $f | sed -e 's/.*_//'`
> > 	mv $f $newf
> > done
> >
> >
> > That should do it.  You can type it in at the command line,
> > or put it in a file. (As always, it is prudent to have a backup
> > before applying wholesale changes to your files.)
> 
> This won't work because there are spaces in the filenames.
> ~M
> P.S.  I'm trying to remember how I solved a similar problem...

How 'bout using parameter substitution...

#!/bin/bash
for f in *
do
    newf=${f##*_}
    mv $f $newf
done

You may want to add a test for the existence of the new file unless you're
certain the extensions are unique.

Sandy
-- 
Sandy Herring, RHCE                        o              sandy at herring.org
Peck of Pickled Pisces               __  o               http://herring.org/
UNIX or Web authoring questions?  |\/ o\  o  http://herring.org/finger.html
=>http://herring.org/techie.html  |/\__/     http://herring.org/pub-key.asc
*sh, Perl, C, VBA, PICK Assembler, Data/Basic, PROC & profanity spoken here.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: <http://lists.pdxlinux.org/pipermail/plug/attachments/20020425/684591b1/attachment.asc>


More information about the PLUG mailing list