I needed to replace a string in several thousand files scattered all over the filesystem on one of our servers. I used find to create a list of files that needed to be changed, along with their complete path and called it “list.txt”. It looked something like this:
/path/to/directory with spaces/filefour.txt
and so on...
I worked out the “sed” command to do the in place editing, and Zach helped me whip up a quick PHP script to read the contents of “list.txt” into an array and iterate through it. He was also nice enough to show me how to use “str_replace” to escape any annoying spaces that happened to find their way into the names of directories.
- < ?php
- foreach($files as $file)
- $command='/bin/sed -i \'s/old-string/new-string/g\' '.str_replace(' ','\ ',$file);
It’s a handy little script that I’m sure I will find a use for later, so I thought I would put it up here.