0

I have a folder which contains a ~50 text files (PHP) and hundreds of images. I would like to move all the images to a subfolder, and update the PHP files so any reference to those images point to the new subfolder.

I know I can move all the images quite easily (mv *.jpg /image, mv *.gif /image, etc...), but don't know how to go about updating all the text files - I assume a Regex has to be created to match all the images in a file, and then somehow the new directory has to be appended to the image file name? Is this best done with a shell script? Any help is appreciated (Server is Linux/CentOs5)

Thanks!

Professor Frink
  • 529
  • 4
  • 7
  • 15
  • 1
    Perhaps StackOverflow can help? Sounds a bit more programming than server-ish. – Coops Mar 04 '11 at 15:12
  • I can try there, but as this involved moving files around the file system / shell scripting, I thought it may be more appropriate here? – Professor Frink Mar 04 '11 at 15:13
  • Don't repost your question. Click on the `flag` link and ask a moderator to move it or wait until enough move votes have been cast. Scripting and automating modification of source files is on-topic for SO. – Dennis Williamson Mar 04 '11 at 15:48

1 Answers1

1

The script below should get you started. Try it on a copy of your data first though.

#!/bin/bash
mkdir images
for f in *.jpg *.gif
do
    sed "s|$f|images/$f|" *.php
    mv $f images
done
user9517
  • 115,471
  • 20
  • 215
  • 297