Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)


share|improve this question
Perhaps StackOverflow can help? Sounds a bit more programming than server-ish. – Coops Mar 4 '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 4 '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 4 '11 at 15:48

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

mkdir images
for f in *.jpg *.gif
    sed "s|$f|images/$f|" *.php
    mv $f images
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.