1

I have a common line in different files present in different folders. how can i replace that line with some other line using shell script sed command in all the files of the folders ?

Eg: t="/home/file.txt" present in one of the file in a folder same line is present in different file of differnt folder

I have to replace it with t="/desk/file1.txt" . so where can i execute the sed command whether outside the folder ?

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
rpav
  • 39
  • 7
  • Possible duplicate of [How to replace a string in multiple files in linux command line](https://stackoverflow.com/questions/11392478/how-to-replace-a-string-in-multiple-files-in-linux-command-line) – LuFFy Nov 30 '17 at 05:29
  • 1
    Welcome to StackOverflow! There are a few ways you could do this, but StackOverflow is about helping you fix your own code, we're not a free consultancy and coding service. Try to solve your own problem, and if/when you run into difficulty, that's the time to ask a question. Please add your attempt, and your results, to your question. Only then can we help you figure out where you went wrong. – ghoti Nov 30 '17 at 06:07

1 Answers1

0
find . -type f -exec sed -i 's/strng1/string2/g' {} +

string1 will be replaced by string2. It will replace all the files in the current directory and its all subdirectories. You need to run this script from parent directory

If you want to work this with both GNU and BSD/Mac, use this

 find . -type f -exec sed -i.bak 's/strng1/string2/g' {} +
Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
  • when i tried this it is asking for input( >). it does not get executed .please help – rpav Nov 30 '17 at 05:40
  • This use of the `-i` option for `sed` will work in GNU sed, common on Linux, but not in other variants (*BSD, macOS, Solaris, etc, etc). If the question isn't platform specific but your solution is, it's a good idea to point that out to reduce confusion. Or better yet, suggest a portable solution. – ghoti Nov 30 '17 at 06:01
  • @ ghoti, Thanks for this info. – Vishnu T S Nov 30 '17 at 06:13