views:

73

answers:

5

i have a renamed js file which i have to call in each of my php page. Now i want to replace that old name with the new one using shell. what iam using is this :-

sed -i ’s/old/new/g’ *

but this is giving the following error :-

sed: -e expression #1, char 1: unknown command:

now how can i do this replacement??

+2  A: 

There are probably less verbose solutions, but here we go:

for i in *; do sed -i 's/old/new/g' $i; done

Mind you, it will only work on the current level of the file system, files in subdirectories will not be modified. Also, you might want to replace * with *.php, or make backups (pass an argument after -i, and it will make a backup with the given extension).

WishCow
A: 

Try this:

ls | grep "php" > files.txt
for file in $(cat files.txt); do
    sed 's/catch/send/g' $file > TMPfile.php && mv TMPfile.php $file
done
lugte098
ls/grep/cat are useless. ` for file in *.php;do .... ;done`
ghostdog74
Don't know about useless - but its a very ugly approach, then there's the fact that using fixed temporary file name (files.txt, TMPfile.php) will break if there's any concurrency.
symcbean
A: 
perl -pi -e 's/old/new/g' *.php
radio4fan
+1  A: 
sed -i.bak 's/old/new/g' *.php

to do it recursively

find /path -type f -iname '*.php' -exec sed -i.bak 's/old/new/' "{}" +;
ghostdog74
A: 

You are using Unicode apostrophes (RIGHT SINGLE QUOTATION MARK - U2019) instead of ASCII (0x27) apostrophes around your sed command argument.

Dennis Williamson