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

Re: OT: Wildcard renaming of files



Martin Fluch <fluch@rock.helsinki.fi> wrote:
>On Sat, 11 Nov 2000 csj@mindgate.net wrote:
>> I have a bunch of files I want to rename from .html to .htm. Is there
>> I way to rename them all at once?
>
>A little bit bash programming...
>
>for $i in *.html ; do mv "$i" "${i:0:${#tmp}-1}" ; done
>
>Yes, I know, that this is quite cryptic ... but unfortunately I'm to tired
>to explain it ... I've need some sleep :-)

Did you mean "${i:0:${#i}-1}"? That's $i with a substring operation to
trim off the last character (${#i} is the length of $i).

Personally, I think that "${i/%.html/.htm}" is a bit more obvious, and
probably easier to extend to other situations too. You could also try
the basename trick:

  for $i in *.html; do mv $i `basename $i .html`.htm; done

... or, if you might have spaces in the filenames:

  for $i in *.html; do mv "$i" "`basename '$i' .html`.htm"; done

Note the careful quoting here. :)

-- 
Colin Watson                                     [cjw44@flatline.org.uk]



Reply to: