Need a file renaming facility
Leo de Wit
leo at philmds.UUCP
Fri May 20 21:05:26 AEST 1988
In article <2956 at ea.ecn.purdue.edu> davy at ea.ecn.purdue.edu.UUCP (Dave Curry) writes:
> ...[stuff deleted]...
>>>Try:
>>> foo% foreach i ( `ls *.pre sed 's/.pre$//'` )
>>> ? echo "Moving ${i}..."
>>> ? mv ${i}.pre $i
>>> ? end
> ...[stuff deleted about someone using basename each time in the loop]...
>
>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.
> ...[further discussion using csh features]...
I agree about not using basename inside the loop (besides, sed is not that
big and you use it only once), but still like the sed solution more;
it is more flexible and more 'Un*x-style': let each tool do it's own dedicated
job. Sed is good at conversions. I used a similar construct to rename
VMS (have to clean my mouth now 8-) filenames to Un*x filenames, i.e.
lowercase conversion, version stripping. Sed can do just that, e.g.
------------- Start Here -------------
#!/bin/sh
set `ls *\;* 2>/dev/null |sed '
p
s/\([^ ]*\);[0-9]*/\1/
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
# The positional parameters now are: oldname1 newname1 oldname2 newname2 etc.
while :
do
case $# in
0) exit 0;;
esac
mv $1 $2
shift; shift
done
------------- End Here -------------
and you can do all other kinds of conversions, too.
Leo.
More information about the Comp.unix.questions
mailing list