Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!ll-xn!mit-eddie!rutgers!iuvax!pur-ee!ea.ecn.purdue.edu!davy From: davy@ea.ecn.purdue.edu (Dave Curry) Newsgroups: comp.unix.questions Subject: Re: Need a file renaming facility Message-ID: <2956@ea.ecn.purdue.edu> Date: 9 May 88 13:24:23 GMT References: <13840@brl-adm.ARPA> Reply-To: davy@ea.ecn.purdue.edu.UUCP (Dave Curry) Organization: Purdue University Engineering Computer Network Lines: 45 In article <13840@brl-adm.ARPA> amoss%HUJINIX.BITNET@cunyvm.cuny.edu (Amos Shapira) writes: >Chris Reimer (ctr@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