Socrates
Socrates

Reputation: 395

using sed to replace a string in all files of a directory

I am trying to find and replace all occurances of this:

 #include <boost

with this:

#include </home/pi/Desktop/boost_1_66_0/boost

for the libraries to work. here is what i came up with:

sed -i 's/#include <boost/#include <\/home\/pi\/Desktop\/boost_1_66_0\/boost/g' *

but gives me this:

sed: couldn't edit build: not a regular file

did i escape the '/' wrong or is # the problem?

Upvotes: 11

Views: 18148

Answers (2)

asterio gonzalez
asterio gonzalez

Reputation: 1204

I think sed is missing the '/g' part to end the command. This works for me

find . -type f -name '*.py' -exec sed -i 's/from/to/g' {} \; -ls

restricting the search with pattern and show which files has been considering.

Upvotes: -1

Turn
Turn

Reputation: 7020

It looks like build is a directory and you can't run sed on a directory. One option, if your directory tree is only one deep is to use a final argument */* instead of *. For an arbitrarily deep directory structure a more effective method is to use find to find all the files and run sed on them:

find . -type f -exec sed -i 's/from/to/' {} \;

This uses find to start in the current directory and recursively descend the directory tree. For each regular file (-type f) it finds, it will run the command given to the -exec option. It substitutes the {} with the files it found.

Upvotes: 21

Related Questions