[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to rename multiple files



On Tue, Dec 10, 2002 at 10:11:20AM -0500, Rick Pasotto wrote:
> On Mon, Dec 09, 2002 at 11:33:03PM -0800, Osamu Aoki wrote:
> > On Mon, Dec 09, 2002 at 10:02:25PM -0600, Shyamal Prasad wrote:
> > >     "drew" == drew cohan <debian@drewcohan.com> writes:
> > > 
> > >     drew> How do I rename all files in a directory matching the
> > >     drew> pattern *.JPG to *.jpg in a bash shell script?  Thanks to
> > >     drew> you guys I can check for the existence of jpgs in a
> > >     drew> directory, but can't seem get 'mv' to rename them for me
> > >     drew> (always complains that the last argument must be a
> > >     drew> directory).
> > > 
> > > Still another way:
> > > 
> > > for i in *.JPG
> > > do
> > >   mv $i `basename $i .JPG`.jpg
> > > done
> > 
> > Simplest alternative without sub-shell nor special command:
> > 
> > for i in *.JPG
> > do
> >   mv $i ${i%\.JPG}.jpg
> > done
> > 
> 
> I've been offline so I haven't seen all the responses but you'd better
> put double quotes around the $i or it will fail if the file name has one
> or more spaces in it.

Very good point (considering these upper case filenames may be in Samba
share drive where people tends to have such insane file names)

Nicest 1 liner shell script is:

if [ -e *.JPG ]; then for i in *.JPG; do mv "$i" "${i%.JPG}.jpg"; done fi

This catches the case where no file with the name *.JPG

Am I correct? I hope so :)
-- 
~\^o^/~~~ ~\^.^/~~~ ~\^*^/~~~ ~\^_^/~~~ ~\^+^/~~~ ~\^:^/~~~ ~\^v^/~~~ +++++
        Osamu Aoki <osamu@debian.org>   Cupertino CA USA, GPG-key: A8061F32
 .''`.  Debian Reference: post-installation user's guide for non-developers
 : :' : http://qref.sf.net and http://people.debian.org/~osamu
 `. `'  "Our Priorities are Our Users and Free Software" --- Social Contract



Reply to: