Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a string in multiple files in linux command line

Tags:

string

linux

I need to replace a string in a lot of files in a folder, with only ssh access to the server. How can I do this?

like image 699
mridul4c Avatar asked Jul 09 '12 09:07

mridul4c


1 Answers

cd /path/to/your/folder sed -i 's/foo/bar/g' * 

Occurrences of "foo" will be replaced with "bar".

On BSD systems like macOS, you need to provide a backup extension like -i '.bak' or else "risk corruption or partial content" per the manpage.

cd /path/to/your/folder sed -i '.bak' 's/foo/bar/g' * 
like image 101
kev Avatar answered Sep 18 '22 12:09

kev