The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
[ HOME | TOPICS | INDEX | WEB RESOURCES | BOOKS | CONTRIBUTE | SEARCH | FEEDBACK | FAQ | FORUMS ]
 New Topic  |  Go to Top  |  Go to Topic  |  Search  |  Log In   Newer Topic  |  Older Topic 
 Re: Massive website changes
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----

Reply To This Message
 Forum List  |  Threaded View   Newer Topic  |  Older Topic 


 Forum List  |  Need a Login? Register Here 
 User Login
 User Name:
 Password:
 Remember my login:
   
 Forgot Your Password?
Enter your email address or user name below and a new password will be sent to the email address associated with your profile.
How to get the most out of the forum

phorum.org