[PLUG] Rename script
Don Buchholz
don at truedisk.com
Tue Apr 30 02:15:47 UTC 2002
"Randal L. Schwartz" wrote:
>
> >>>>> "Don" == Don Buchholz <don at truedisk.com> writes:
>
> Don> Add quotes around the two args to mv(1):
> Don> #!/bin/sh
> Don> for f in *
> Don> do
> Don> newf=`echo $f | sed -e 's/.*_//'`
> Don> mv "$f" "$newf"
> Don> done
>
> Don> If you have double-quotes (") in the file name, well, all bets are off.
>
> And newlines!
>
> (Yes, newlines are *legal* in filenames. Caveat Executor.)
>
> Perl, of course, handles this stuff with ease, and no whitespace worries.
>
Well, I *was* assuming the W at reZ crew didn't create the files. :-P :-)
That is an important caveat. Carraige returns can be really
fun in a name, too! For example:
touch `perl -e 'print "try_and_\rDELETE_ME!";'`
I notice the Linux ls(1) and dir(1) are pretty robust. But "ls | cat"
makes life a bit more interesting. (And, I must credit a WareZ group
for really hammering home the little lesson that a filename can contain
*anything* except '/' or '\0'. May their slimy corpses roast in Hades.)
Now, can Perl do it in nice quick one-liner as elegant as Sandy's
variable expansion trick? (Sandy's method is handling the "\r" case,
and the space " " case pretty well, it seems.) I must confess to not
using Perl as much as I ought to -- but the ubiquity of "sh" is pretty
much unparalled, so I tend to use it.
- Don
More information about the PLUG
mailing list