简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM