20

We are converting all the static html pages in our codebase into php pages. The first step would be to change all the .html file extension to .php (which I already did). The second step would be to update all the links within each of those html pages to point to new php pages.

(for instance, inside index.php i have links to both contact.html and about-us.html. Now since we have replaced every .html file extension to .php, we need to change contact.html to contact.php, and likewise, about-us.html to about-us.php).

what i want to do now is to search for a particular string across multiple files. (search for "contact.html" inside many files, such as index.php, index2.php, index3.php, etc etc..) after that, replace all "contact.html" in all those files with "contact.php".

I am not familiar with unix command line, and i so far have seen other people's similar questions here in the forum but not quite understand which one could help me achieve what i want. I'm using cygwin and if possible i need to solve this without perl script since i dont have it installed. i need to try using either sed, grep, find, or anything else.

So, if any of you think that this is a duplicate please point me to a relevant post out there. thanks for your time.

6 Answers 6

37

Try this:

find . -name '*.html' -exec sed -i 's/\.html/\.php/g' "{}" \;

It will find all files in the current and subdirectories that end in .html, and run sed on each of them to replace .html with .php anywhere it appears within them.

See http://content.hccfl.edu/pollock/unix/findcmd.htm for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

This works with GNU sed; it won't necessarily work on other Unix platforms because of the non-standard -i option to sed.
@JonathanLeffler: Yes, how spoiled we become. :-)
15

On my Mac, I had to add a parameter to the -i option: the extension to add to the name (in this case, nothing, so the file is stored in-place).

find . -name '*.html' -exec sed -i '' 's/\.html/\.php/g' "{}" \;

Comments

1
You can certainly use ack to select your files to update. For example,
to change all "foo" to "bar" in all PHP files, you can do this from 
the Unix shell: 

perl -i -p -e's/foo/bar/g' $(ack -f --php)

Source: man ack

1 Comment

This solution preserved end-of-file newlines on OS X, where sed would remove them. (I had to brew install ack though)
0

This worked for me :

find . -type f -print0 | xargs -0 sed -i 's/str1/str2/g'

Comments

0

This should work (found here: http://www.mehtanirav.com/search-and-replace-recursively-using-sed-and-grep/):

for i in `find . -type f` ; do sed "s/html/php/g" "$i" >> "$i.xx"; mv "$i.xx" "$i"; done

Comments

0

Below script can help in replacing the string in files:

#!/bin/bash
echo -e "please specify the folder where string has need to be replaced: \c"
read a
echo -e "please specify the string to be replaced: \c"
read b
echo -e "please specify the string: \c"
read c
find $a -type f -exec sed -i 's/'"$b"'/'"$c"'/g' {} \; > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "string has been replaced..."
else                                                                         echo "String replacement has been failed"
exit0
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.