I wrote:
ersonally I use a shell for loop with mv and basename,
like:
for f in *.foo; do mv $f `basename $f .foo`.bar; done
That won't work when you want to change something other
than the suffix of a file,
Rich Alderson wrote:
For this kind of thing, I revert to tcsh (explicitly
invoked
from the bash prompt):
% foreach F (*.foo)
set R=$F:r
mv $F $R.bar
end
All kinds of editing can be accomplished with :h, :t, :r,
and :e, so it's easy to change things other than the suffix.
(I have been using ksh and bash for 20 years, and still
can't remember the equivalents of tcsh's editing modifiers.
Bash does have pattern matching in variable expansion. Changing the
suffix can be done by:
for f in *.foo; do mv $f ${f/%.foo/.bar}; done
Which is arguably simpler than my earlier example using command
substitution (backtick) with basename. The part between the slashes is
the "pattern", and the part after the second slash is the replacement
string. Patterns are not full regular expressions, but just the usual
shell globbing characters. The '%' at the beginning of the pattern
means to match the pattern only at the end of the string; you can use a
'#' to match at the beginning, or neither to match anywhere.
I also sometimes have to rename files from mixed case or all caps to all
lower case, and bash has an expansion feature useful for that, too:
for f in *; do mv $f ${f,,}; done
',,' means convert all characters to lower case. '^^' would convert to
all upper case.
There's obviously a lot more that bash can do than what I've described.
For the fancier stuff I always wind up RingTFM.
Eric