Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
1  
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
add comment

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

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.