Need a file renaming facility
Dave Curry
davy at ea.ecn.purdue.edu
Mon May 9 23:24:23 AEST 1988
In article <13840 at brl-adm.ARPA> amoss%HUJINIX.BITNET at cunyvm.cuny.edu (Amos Shapira) writes:
>Chris Reimer (ctr at stride.com) writes:
>>
>>Try:
>> foo% foreach i ( `ls *.pre sed 's/.pre$//'` )
>> ? echo "Moving ${i}..."
>> ? mv ${i}.pre $i
>> ? end
>>
>>Obviously (I hope), this must be run under csh. Enjoy!
>
> Could make it simpler, and much more importnt, faster.
> My suggestion is to use basename(1), like this:
>
> % foreach i (*.pre)
> ? echo Moving $i...
> ? mv $i `basename $i .pre`
> ? end
>
> basename(1) is under /usr/bin, so I'm not sure if it
> comes with SV systems. But you can write one for yourself.
If you're going for speed, then forking and execing basename for every
file name is certainly not the solution. I suspect for any number of
files greater than 3 or 4, the ls/sed answer is faster.
But, both of you are doing things the wrong way. If you're going to
use "csh", then instead of simply writing an "sh" script in "csh"
syntax, you might as well use some of "csh"'s useful features, namely
the colon modifiers (or whatever they're called):
% foreach i (*.pre)
? echo Moving $i...
? mv $i $i:r
? end
The ":r" operator removes the first (working from the right)
".anything" from a file name. It's exactly equivalent to your
basename version above, only about a zillion times faster. There are
other colon modifiers which let you take the basename of a file (:t),
the directory part of a file name (:h), and the extension (.anything)
part of a file name (:e). And yes, it's all documented in the manual
page, this isn't secret magic stuff.
--Dave Curry
More information about the Comp.unix.questions
mailing list