3

I have an online website that I'd like to move to a new URL. Therefore I need to edit all website html files (100+ files) to update the URL links inside each file of this website.

I'm thinking of making an off-line copy of the website files to edit.

Is it possibile to use Automator OS X to - open each file in the website file folder - search inside the file content for a specific URL link (say "www.website.com") and change it to the new URL (say "www.website.com/old")?

Else, do I need to rely on a text editor + Automator to make this process?

Or, can it be done using a UNIX script that you run in the Terminal ?

Thanks for any advice on how to perform this process!

  • You can use sed in Terminal, but for a specific answer we would need to see an excerpt of one of the web pages showing the old and the new URL. – nohillside Jul 21 '17 at 13:12
  • I did something similar a while back but did it in BBEdit as it can find/replace across multiple files. In case you are interested in commercial ($) options. – Steve Chambers Jul 21 '17 at 15:55
  • Did any of the solutions below help you? – ʀ2ᴅ2 Jul 25 '17 at 12:49
3

Assuming all files are in the same folder:

mkdir new
for i in *.html; do
    sed 's|website.com|website.com/old|g' "$i" > "new/$i"
done
2

If you're commonly editing website file contents you should invest in an editor.

Sublime Text 2

Drop the website onto the icon and it will build the project:

enter image description here

run cmdshiftF and enter the replacement:

enter image description here

Click the Replace button and verify the Replace:

enter image description here

It will open and change all the files:

enter image description here


BBEdit

Is an excellent and robust application I primarily use to edit code. Drag and drop the folder onto BBEdit and go to File -> Save Project and it will create a file named foobar.bbprojectd:

enter image description here

run cmdshiftF and select the project with the replacement parameters:

enter image description here

It will prompt you after clicking Replace All:

enter image description here

If you are unsure on your changes select Leave Open because if you select Save to disk it is very hard to revert back.


Perl

There is already a sed solution but you could also do the same with Perl:

# !/bin/bash

DIR="/Users/vader/desktop/test"
NEW="$DIR/new"

if [ ! -e "$DIR/new" ]; then 
    mkdir -p new
    echo "made folder new"
else
    echo "folder exists"
fi

for htmlfile in $(ls "$DIR"/*.html); do
    TEMPFILE="$htmlfile.$$"
    htmlbase=$(basename $htmlfile)
    echo "Working on "$htmlbase
    perl -pe 's|www.website.com|www.website.com\/old|g' "$htmlfile" > $TEMPFILE && mv $TEMPFILE "$NEW/$htmlbase"
done

There are other editors out there but these are the two I primarily use. You could also do this in pure AppleScript but you haven't shown any code and mentioned terminal so I made the above in Perl.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .