How to replicate VSCode's 'search and replace' code in Bash?

Viewed 22

I have a project that contains one file a.txt that contains the text hello, and a folder called b that contains a file c.txt that also contains the text hello. I want to run a bash command that will replace these two instances of hello with goodbye, identical to VSCode's search-and-replace functionality.

I've tried sed -i '.bak' 's/hello/goodbye/g' *, but it gives me the error sed: folder: in-place editing only works for regular files.

How should I approach this? I'm using MacOS.

1 Answers

you can use find to locate the files you need to edit and the --exec param to run your sed command. Something like

find . -name '*.txt' -type f -exec sed -i '.bak' 's/hello/goodbye/g' {} \;
Related