39

I have a renamed js file which I have to call in each of my php pages. 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:

How can I do this replacement?

Kolay.Ne
  • 1,104
  • 6
  • 16
developer
  • 1,952
  • 10
  • 35
  • 58

8 Answers8

54
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
  • 286,686
  • 52
  • 238
  • 332
  • @WimPruiksma, I get the same error from BSD sed on the mac, but not from GNU sed (installed as gsed with brew). – DaCheetah Mar 19 '18 at 07:00
  • 1
    sed -i is not POSIX standard required, so it's not in OS X or BSD iirc. – Wyatt Ward May 02 '18 at 14:33
  • FTR: I've just replaced in place with sed and FreeBSD 11.3 using `sed -i '' -e '...'`, as described in answers to https://stackoverflow.com/questions/12696125/sed-edit-file-in-place – Piohen Jul 22 '20 at 21:29
33

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).

K. Norbert
  • 10,138
  • 4
  • 45
  • 46
  • 1
    on OSX you have BSD sed, and need to pass a file extension for backups to the -i option. Check out [this page](https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux) – spectrum Jan 19 '19 at 16:24
  • I get `sed: couldn't edit includes: not a regular file` for many folders. – Black Jul 01 '19 at 14:34
  • I assume they are FIFOs or device files, or something other than a regular file. If you have non-regular files, you will have to use something else instead of a glob (like find -type f ...). – K. Norbert Jul 01 '19 at 15:10
10

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

Dennis Williamson
  • 303,596
  • 86
  • 357
  • 418
10

this one is very simple, without for or loop, and takes care of any number or nested directories

grep -rl 'oldText' [folderName-optional] | xargs sed -i 's/oldText/newText/g'

Martin Pecka
  • 2,482
  • 1
  • 26
  • 34
  • I get `No such file or directory` with this one and recursive directories. – dman Nov 06 '18 at 14:02
  • @dman probably you have executed it with [folderName-optional] without changing it . you may remove it, or can keep only single dot without quotes `grep -rl 'oldText' . | xargs sed -i 's/oldText/newText/g' ` –  Nov 06 '18 at 15:01
8

I know I'm really late but still:

find . -type f -name "*.php"|xargs sed -i 's/old/new/g'
Unheilig
  • 15,690
  • 193
  • 65
  • 96
Ziyad
  • 147
  • 1
  • 7
4
perl -pi -e 's/old/new/g' *.php
radio4fan
  • 176
  • 4
2

For completeness, providing the OSX compatible version of the above accepted answer (to answer comment from @jamescampbell)

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

This command creates .orig backup files.

shiramy
  • 517
  • 5
  • 11
-3

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
  • 2,013
  • 5
  • 20
  • 28
  • 1
    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 Mar 26 '10 at 12:17