Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: help with 'rename' function



>>>>> "Peter" == Peter Stephenson <pws@xxxxxx> writes:

    Peter> Timothy J Luoma wrote:
    >> I used to have a binary that did this, but I've lost it.
    >> 
    >> What it did was simple: it changed extensions of filenames.

    Peter> It's perennially annoying UNIX doesn't have this.  On the
    Peter> other hand, if it did it would probably be so inscrutable
    Peter> as to be almost useless.

I have been using the DOS style rename under Unix for years, i.e.,

(1)ezdzit lew x>> touch {a,b,c,d,e}.c
(1)ezdzit lew x>> ls
a.c   b.c   c.c   d.c   e.c
(1)ezdzit lew x>> ren *.c *.d
(1)ezdzit lew x>> ls
a.d   b.d   c.d   d.d   e.d

Yes, this can be done under Unix with some tweak.  The following alias
and function definition actually called a csh script to do the real
work which can be converted to zsh.  As you can see I have been using
this before the birth of zsh.

     alias ren='set -F; rena'
     function rena () {
        set -F
        renam $*
        set +F
	}

#! /bin/csh -f
#
#-	rename - VMS style rename used for wildcard renaming
#-
#-	This is to facilitate some desirable VMS feature on Unix. For
#-	example:
#-		$ mv *.c *.c00
#-		$ mv abc.* def.*
#-
#-	Another tool 'move' which use sed to change file  name is more
#-	general purpose  than  'rename' or the 'rename.net' written by
#-	Juergen Wagner <gandalf@xxxxxxxxxxxxxxxxx>.
#-
#	Author:		Paul Lew, General Systems Group, Inc.
#	Created at:	03/02/88  01:48 PM
#	Last update:	07/29/88  06:01 PM  (Edition: 23)
#
#-	Usage:		rename old new
#-	where: there must be one '*' in both old and new
#-
#---------------------------------------------------------------#
#	      Display help if requested by user			#
#---------------------------------------------------------------#
switch ( "$1" )
	case -H[xX]:
		set echo; set verbose; shift
		breaksw
	case -H*:
		show_help `which $0` $1
		goto end
	default:
	endsw
#---------------------------------------------------------------#
#			Process Arguments			#
#---------------------------------------------------------------#
if ( $#argv != 2 ) then
	show_help `which $0`
	goto end
	endif
#---------------------------------------------------------------#
#	    Process each item in the argument list		#
#---------------------------------------------------------------#
set src = "$1"
set dest = "$2"
set cmd
set noglob
if ( ( "$1:r" != '*' && "$1:e" != '*' ) || \
     ( "$2:r" != '*' && "$2:e" != '*' ) ) then
	echo "wildcard characters required on both src and dest"
	goto end
	endif
if ( "$src:r" == "$dest:r" && "$src:r" == '*' ) then
	set same_root
else if ( "$src:e" == "$dest:e" && "$src:e" == '*' ) then
	set same_ext
else if ( "$src:r" == "$dest:e" && "$src:r" == '*' ) then
	set r_to_e
else if ( "$src:e" == "$dest:r" && "$src:e" == '*' ) then
	set e_to_r
	endif
unset noglob
set nonomatch
foreach file ($src)
	if ( "$file" == "$src" ) then
		echo "...File: $file not found, aborted..."
		goto end
		endif
	if ( $?same_root ) $cmd mv $file ${file:r}.${dest:e}
	if ( $?same_ext  ) $cmd mv $file ${dest:r}.${file:e}
	if ( $?r_to_e    ) $cmd mv $file ${dest:r}.${file:r}
	if ( $?e_to_r    ) $cmd mv $file ${file:e}.${dest:e}
	end
#---------------------------------------------------------------#
#		Clean up and exit here...			#
#---------------------------------------------------------------#
end:
unset src dest file



Messages sorted by: Reverse Date, Date, Thread, Author