Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace a particular text in all the files using single line shell command

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?

like image 714
developer Avatar asked Mar 26 '10 11:03

developer


People also ask

How do I replace text in multiple files?

Remove all the files you don't want to edit by selecting them and pressing DEL, then right-click the remaining files and choose Open all. Now go to Search > Replace or press CTRL+H, which will launch the Replace menu. Here you'll find an option to Replace All in All Opened Documents.

How do I replace text in a file in bash?

To replace content in a file, you must search for the particular file string. The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.

How do you replace a word in all files in a directory Linux?

s/search/replace/g — this is the substitution command. The s stands for substitute (i.e. replace), the g instructs the command to replace all occurrences.


1 Answers

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/' "{}" +; 
like image 130
ghostdog74 Avatar answered Sep 21 '22 22:09

ghostdog74