simple question about mv

Geoff Clare gwc at root.co.uk
Thu Feb 2 21:30:02 AEST 1989


In article <18216 at adm.BRL.MIL> DEG41560 at ua1vm.ua.edu (Rob Smith) writes:
>
>What if I want to mv a bunch
>of files with the same suffix to another suffix. The following does not
>work
>
>    mv *.flip *.flop
>
>what does?  I'm under Ultrix 2.2.  I'm doing it by hand, but I'd like to
>be able to do wild card renames, ala DOS.
>

The following is a 'rename' script which I find very useful. It allows
you to apply any 'sed' command to a list of files.  To safeguard
against duplicate new names it does not allow existing files to
be overwritten (except if 'mv' will query them).  It gives a running
commentary as it renames each file.

The job above becomes:
	
	rename 's/flip$/flop/' *.flip

More examples:

	rename 's/abc/xyz/g' *abc*
	rename 's/abc\(.*\)def/uvw\1xyz/' *
	rename 'y/abc/ABC/' *[abc]*

Enjoy,
	Geoff.

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
-----cut here-----cut here-----cut here-----cut here-----
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	rename
# This archive created: Thu Feb  2 11:04:51 1989
echo shar: extracting rename '(897 characters)'
sed 's/^X//' << \SHAR_EOF > rename
X:
X#  rename.sh	Geoff Clare	Jan 1987
X
X#  Safely rename multiple files by applying a given 'sed' command
X#  to a list of file names
X
XUSAGE='Usage: rename sed_command file ...'
X
Xif test $# -lt 2
Xthen
X	echo >&2 "$USAGE"
X	exit 2
Xfi
X
X# Safeguards for existing files rely on 'mv' interactive behaviour
Xif test ! -t 0
Xthen
X	echo >&2 "rename: must be used interactively"
X	exit 2
Xfi
X
X# Dup standard input to use in 'mv' command
Xexec 3<&0
X
XCMD="$1"
Xshift
X
X# Let 'ls' spot non-existent files for us
X# The first 'p' command prints the old name on a line before the new name
X
X/bin/ls -d "$@" | /bin/sed -e p -e "$CMD" | while read old
Xdo
X	read new || new="$old"
X
X	if test "X$old" = "X$new"
X	then
X		echo >&2 "$old: no change"
X	elif test -w "$new"	# 'mv' will query non-writable files
X	then
X		echo >&2 "$old not renamed: $new already exists"
X	else
X		echo >&2 "$old --> $new"
X		/bin/mv 0<&3 "$old" "$new"
X	fi
Xdone
SHAR_EOF
if test 897 -ne "`wc -c rename`"
then
echo shar: error transmitting rename '(should have been 897 characters)'
fi
#	End of shell archive
exit 0
-- 

Geoff Clare    UniSoft Limited, Saunderson House, Hayne Street, London EC1A 9HH
gwc at root.co.uk   ...!mcvax!ukc!root44!gwc   +44-1-606-7799  FAX: +44-1-726-2750



More information about the Comp.unix.questions mailing list