Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!mcnc!rti!bdrc!jcl From: jcl@bdrc.COM (John C. Lusth) Newsgroups: comp.unix.questions Subject: Re: Need a file renaming facility Message-ID: <324@bdrc.UUCP> Date: 10 May 88 15:37:13 GMT References: <3564@fluke.COM> Reply-To: jcl@bdrc.UUCP (John C. Lusth) Organization: Becton Dickinson Research Cntr., Research Tri. Park, NC Lines: 54 >In article <3564@fluke.COM> inc@tc.fluke.COM (Gary Benson) writes: > > QT.1.r QT.4.r QT.A.r > Qt.1.r.pre QT.4.r.pre QT.A.r.pre > >Now I want to rename all those ".pre" files to the same name without ".pre". Here is a shell script that renames portions of filenames: #!/bin/csh ######################################################### # # mv+ pattern1 pattern2 file1 [ file2 ... ] # # rename files by replacing the last pattern1 with pattern2 set x = $1 # what it was shift set y = $1 # what it shall be shift foreach f ( $* ) if ( `expr match $f '.*'$x'.*'` ) then set a = `expr match $f '\(.*\)'$x'.*'` set b = `expr match $f '.*'$x'\(.*\)'` echo mv $f $a$y$b mv $f $a$y$b else echo $f not moved. endif end echo finished. #################################################################### If you were to name this script mv+ ( I for the life of me can't think up a good name for it), the following command would accomplish your task mv+ .pre "" * Every once in a while, this shell script can be quite useful. You can also do things like mv+ "[abc]" z * which replaces the last letter a, b, or, c with z in any file names containing an a, b, or, c. Messing around with regular expressions can be hazardous though. In general, one should use literals for pattern1. John C. Lusth Becton Dickinson Research Center Research Triangle Park, NC ...!mcnc!bdrc!jcl