Need a file renaming facility

John C. Lusth jcl at bdrc.COM
Wed May 11 01:37:13 AEST 1988


>In article <3564 at fluke.COM> inc at 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



More information about the Comp.unix.questions mailing list