Path: utzoo!attcan!uunet!mcvax!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.unix.questions Subject: Re: Need a file renaming facility Keywords: sed,filename conversion Message-ID: <485@philmds.UUCP> Date: 20 May 88 11:05:26 GMT References: <13840@brl-adm.ARPA> <2956@ea.ecn.purdue.edu> Reply-To: leo@philmds.UUCP (L.J.M. de Wit) Organization: Philips I&E DTS Eindhoven Lines: 44 In article <2956@ea.ecn.purdue.edu> davy@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.