recursive saving of files and folders

Hi all

I have a bash script, that loops through a folders files and all subfolders for .shtml files.

It looks for a string and replaces it, creating a backup of the original file.

This all works great, but I'd like, when the backup is done, that the files are then also created in their respective folders.

So if example.shtml is found in /var/www/files/example/example.shtml
And the backup folder is /var/www/files/backup

Then the file should be in /var/www/files/backup/example/example.shtml

My current code is:

#!bin/bash
OLD="This is a"
NEW="I am a"
BPATH="../backup"

find . -name '*.shtml' -type f |
while read filename
do
    /bin/cp -f $filename $BPATH
    sed -i "s/$OLD/$NEW/g" $filename
done

All help muchly welcome

Regards

#!bin/bash
OLD="This is a"
NEW="I am a"
BPATH="../backup"

find . -name '*.shtml' -type f |
while read filename
do
    RPATH="`dirname $filename`"
    mkdir -p "$BPATH/$RPATH"
    /bin/cp -f $filename "$BPATH/$RPATH"
    sed -i "s/$OLD/$NEW/g" $filename
done  

Perfect!

Thank you

On another note, I want to replace:

OLD="This is a"

with a URL, but I keep getting the following error:

sed: couldn't open file ww.google.co.uk//g: No such file or directory

Do i need some kind of regex or can sed handle urls in terms of strings?

Thanks

Think you should another separator than '/' for sed, something that isn't anyway in an url. You can try with '|' so sed -i "s/$OLD/$NEW/g" $filename would become sed -i "s|$OLD|$NEW|g" $filename

Ah, I wasn't sure i could use another separator other than '/'.

But replacing '/' with '|' worked a charm!

Thank you!