How to replace php3 with "php" inside every file in a directory

Hope a nice simple one.

I have a php3 site that I want to run on php 5.2

For starters, I would just like to have every link to the current "index.php3" _within_each_file_ (recursively) be changed to "index.php" and then move on to the global value question and so on.

K. Go!

:)

Update: Thanks a lot! I understand that my question missed the fact that links are listed in every file of the website and I wanted to do this recursively across all files / folders.

+1


source to share


4 answers


find -type f -exec perl -pi -e 's/\bindex\.php3\b/index.php/g' {} \;

      



+5


source


What about:

for file in *.php3
do
    sed 's/\.php3/.php/g' $file > ${file%3}
done

      

The for / do / done loop processes each .php3 file in turn, editing the file and replacing each line .php3

with .php

, and copying the file from something.php3 to something.php.

Added for modified question:

for file in $(find . -type f -name '*.php3' -print)
do
    sed 's/\.php3/.php/g' $file > ${file%3}
done

      



You can usually omit " -print

" these days , but historically a lot of machine time has been wasted by people who forgot to add it because initially (pre-POSIX) no default action was taken.

This snippet contains the simplest modification of the source code to accomplish an advanced task, but not necessarily the best way to do it - it pre-generates the entire list of files before processing. You can use instead:

find . -type f -name '*.php3' -print |
while read file
do
    sed 's/\.php3/.php/g' $file > ${file%3}
done

      

However, both modified versions can run into problems if file names or path names contain spaces or newlines or other similar characters. To avoid such problems, you need to work harder - using Perl or (GNU find + xargs) or some other methods. If you are sane, you avoid such problems. If you've inherited a stricter customization, then you may need to delve deeper into the arcana of Unix filename handling.

+4


source


sed -i 's/php3/php/g' *

Assuming you are using a UNIX-like operating system.

+2


source


To do it recursively, use Jonathan's answer, but replace find . -type f

with * .php3. Note that back ticks are important and you can replace any directory with "." (i.e. the current directory). Here is a version that also renames the file in the process (* .php3 -> * .php).

#!/bin/bash
for file in `find . -type f`
do
    sed 's/\.php3/\.php/g' $file > ${file//3/}
    rm $file
done

      

+1


source







All Articles