Re: shell scrpt to rename



How is someone who does not write shell script to begin with supposed
to extraoplate the awnser to his question from the following?

23. How do I rename *.foo to *.bar?

Naive examples in ksh/bash (which may or may not work many times)

$ ls *.foo | while read f;do mv "$f" "${f%.*}".bar

More generically

$ ls *.foo | while read f;do mv "$f" `basename "$f" .foo`.bar

However, these examples contain a potentially unnecessary use of
ls (ie, if the number of files is small enough to not overflow the
command line buffer), and will fail if any file names contain a
newline, or if there are leading or trailing spaces. An
alternative is:

for file in *.foo
do
mv -- "$file" "`basename -- \"$file\" .foo`.bar"
done

Also, tests for existence of files should also be incorporated,
e.g.:

for file in ./*.foo
do
newfile=`basename "$file" .foo`.bar
[ -f "$file" ] || continue
[ -f "$newfile" -o -d "$newfile" ] && continue
mv "$file" "$newfile"
done

In some linux distributions you may be able to use the rename
command

$ rename .foo .bar *

If not (Debian, for one, comes with a perl version of rename that
won't work with that command line) try

$ rename 's/.foo/.bar/' *.foo

More options, and much more discussion about this, is available
from http://www.faqs.org/faqs/unix-faq/faq/part2/section-6.html

Note that for file specifications which don't match existing
files, the shell usually responds with something like "ls: *.foo:
No such file or directory", which will mess up your processing of
file names. One possibility is

#! /bin/sh
set x [*].foo ./*.foo
case "$2$3" in
"[*].foo./*.foo") ;;
*)
shift 2
for file
do
repl=`basename "$file" .foo`.bar
mv "$file" "$repl"
done;;
esac

Except that contrary to (zsh) mmv or zmv it doesn't check for
file overwriting and fails for filenames with NLs before the "."
and doesn't handle dotfiles.

.



Relevant Pages

  • Re: ls and backslash - Solaris problem?
    ... > ls foo* would list it, but ls foobar.jpg would fail. ... > used wildcards to ftp the files over. ...
    (comp.unix.solaris)
  • [ANN] main-2.6.0
    ... a class factory and dsl for generating command line programs real ... option 'foo' ... improved auto usage generation when arity is used with arguments ... cast:int ...
    (comp.lang.ruby)
  • [ANN] main-2.8.3
    ... a class factory and dsl for generating command line programs real ... def runputs 'installing...' ... and auto-generation of usage messages can really ... int(foo): the cast is int, ...
    (comp.lang.ruby)
  • [ANN] main-2.2.0
    ... a class factory and dsl for generating command line programs real quick ... def runputs 'installing...' ... and auto-generation of usage messages can really streamline ... int(foo): the cast is int, ...
    (comp.lang.ruby)
  • [ANN] main-2.5.0
    ... a class factory and dsl for generating command line programs real ... def runputs 'installing...' ... and auto-generation of usage messages can ... int(foo): the cast is int, ...
    (comp.lang.ruby)