Author: Dan Larsson
Date: 05-09-01 15:57
One could use the below script for a more complete
version of the script.
For a downloadable version of the script, here's the URL
http://tyfon.net/download/bsd/scripts/extmv.sh
Comments welcome!
---8<----
#!/bin/sh
# description:
# ------------
# this shell script is a little helper
# when you wish to change the filename
# extension on a lot of files.
#
# The intended use is when moving for
# example from .html to .php
#
# hopefully it will correctly change the
# hyperlinks inside the files also :-)
# usage:
# ------
# put the script in ~/bin and cd to a
# directory containing the files you want to change.
# From there just start the script and let
# it do the rest.
# known bugs:
# -----------
# the script is regex case sensitive, meaning
# it sees .HTML and .html as two different filename
# extensions. This might be an issue.
#
# the script assumes all hyperlinks are enclosed
# with doublequote characters (").
# filename extension of the files
# we want to change
oldext=php
# filename extension of what we
# wish to give the changed files
newext=phtml
# where to keep the backups of
# the old files
bkpdir=backup
# do some very basic checks before
# we begin working
if [ ! -w . ] ; then
echo "===> oy, we need write permission!"
exit 1
elif [ -e ${bkpdir} -a ! -d ${bkpdir} ] ; then
echo "===> backup dirname occupied by other file"
exit 1
elif [ -e ${bkpdir} -a -d ${bkdir} ] ; then
echo "===> saving old backup dir"
mv ${bkpdir} ${bkpdir}.$(date +%s)
mkdir ${bkpdir}
elif [ ! -e ${bkpdir} ] ; then
mkdir ${bkpdir}
fi
# loop through each file with
# the old extension
for file in *.${oldext} ; do
if [ -f ${file} ] ; then
echo -n "+ ${file}"
mv ${file} ${bkpdir}
oldfile=${bkpdir}/${file}
newfile=$(echo ${file} | \
sed s/\.${oldext}$/.${newext}/)
sed \
-e "s!\.${oldext}\">!.${newext}\">!g" \
-e "s!\.${oldext}\(\?.*\)\">!.${newext}\1\">!g" \
< ${oldfile} > ${newfile}
echo " -> ${newfile}"
fi
done
# we're done
echo -e "\noriginals are in the '${bkpdir}' directory"
exit 0
--->8----
|
|