>> > I have a bunch of files like "a.HTM, a.HTM" etc. I want to rename
>> > them "a.html, b.html".
This general rename script that I just wrote should do it too:
rename *.HTM =.html
Bo Siltberg
#!/bin/sh -fh
#
# Rename parts of file names.
#
# Created: 97-10-25 BY Bo Siltberg REV. A
#
# Format: rename inputfiles outputfile
#
pgmname=`basename $0`
usage()
{
more << END_HELP
Rename a number of files.
Format: $pgmname infiles... outfile
The file names are split into units. An "=" in the output unit is
replaced with the corresponding name from the input unit.
Example:
$pgmname mfm.code =R6A.= -> mfmR6A.code
$pgmname mfm.code bus.= -> bus.code
$pgmname mfm.code ==.= -> mfmmfm.code
$pgmname mfm.code.gz bus.= -> bus.code.gz
$pgmname mfm.code.gz =.object.= -> mfm.object.gz
However, the basic idea with this command is to rename a number of
files:
$pgmname mfm.* bus.= -> All files starting with 'mfm.' are
given the prefix 'bus.' instead.
END_HELP
Quote:}
changefilename()
{
if [ $# != 2 ]; then # take input from pipe
read f1 f2
else # take input from arguments
f1=$1
f2=$2
fi
\echo "$f1\n$f2" | \
nawk '{
if ( NR == 1 ) {
n1 = split( $0, ins, ".")
input = $0
}
else {
n2 = split( $0, out, ".")
output = $0
}
}
END {
if ( n1 < n2 ) {
printf "%s: Output name '"'%s'"' has more units than input name '"'%s'"'.\n",
program, output, input | "cat >&2"
exit 2
}
outline = ""
for ( i = 1; i <= n2; i++ ) {
gsub("=", ins[i], out[i])
outline = outline "." out[i]
}
for ( i = n2 + 1; i <= n1; i++ ) {
outline = outline "." ins[i]
}
print substr(outline,2)
}' program=$pgmname
return $?
Quote:}
if [ $# -lt 2 ] || [ "X$1" = "X-h" ] || [ "X$1" = "X-help" ]; then
usage
exit 1
fi
# Last arg is output name
inputfiles=""
while [ $# -gt 0 ]; do
n=`echo "$1" | nawk '{ print NF }'`
if [ $n != 1 ]; then
echo "Invalid file name: '$1'."
exit 2
fi
if [ $# = 1 ]; then # Last argument in 'output'
output=$1
else
inputfiles="$inputfiles $1"
fi
shift
done
# Go through input names and check that they are ok
outputfiles=""
count=0
for file in $inputfiles; do
count=`expr "$count" + 1`
base=`basename $file`
dir=`dirname $file`
renameto=`changefilename $base $output`
rc=$?; if [ $rc != 0 ]; then exit $rc; fi
if [ -f $dir/$renameto ]; then
exists=" [ALREADY EXISTS]"
else
exists=""
fi
echo "Rename: $file -> $dir/$renameto $exists"
outputfiles="$outputfiles $dir/$renameto"
done
\echo "Confirm rename of $count files Y/N: \c"
stty raw
ans=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty -raw
echo ""
if [ "X$ans" != "Xy" ]; then
echo "Cancelled."
exit 1
fi
count=0
for file in $inputfiles; do
count=`expr "$count" + 1`
out=`echo "$outputfiles" | nawk '{ print $'$count' }'`
\mv -i $file $out
rc=$?; if [ $rc != 0 ]; then exit $rc; fi
done
exit