Based on the helpful answers I received here, I created the following script, named it convert_image_paths.command, put it in a website folder on my mac and double-clicked it.
#!/bin/bash
# This script will operate on valid relative image paths at this level and one sub-level down, across .html and .css files.
find . -name "*.css" -o -name "*.html" -exec sed -i '' 's/\.\.\/images\//images\//g' {} ';'
find . -name "*.css" -o -name "*.html" -exec sed -i '' 's/images\//http:\/\/mycdn\.com\/images\//g' {} ';'
Unfortunately, instead of changing the relative image paths in the website to the new cdn url, it
- Doubled up the url: http://mycdn.com/http://mycdn.com/images/myimage.png
- Did that for EVERY html/css file on my machine
So my question is a) is there a tag for moron and b) how do I fix this script?
EDIT:
Let me clarify my intention:
I wanted to apply the change to all image paths with relative urls in the form
“image/{myimagepath}”
or
“../images/{myimagepath}”
To answer your second question:
b) Don’t do “find .” when you don’t know what directory the script is starting from. If you want it to start from a particular directory, tell it in the find command:
find /Users/ptomblin/Shared/ ...As for why it doubled up the URL, it’s because you told it to.
Takes every instance of the word “images/” and changed it to “http://mycdn.com/images/”, even if it already started with “http://mycdn.com/”. If that’s not what you wanted, you’re going to have to be more specific about your regexp.
As a first attempt, I would replace BOTH lines with the following
That will only replace
imagesor../imagesif they start with a double quote.