LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Change a string recursively in files located in folders and subfolders (https://www.linuxquestions.org/questions/linux-newbie-8/change-a-string-recursively-in-files-located-in-folders-and-subfolders-893183/)

Drigo 07-22-2011 12:30 PM

Change a string recursively in files located in folders and subfolders
 
So, lets say I have a project that have generated lots of xml files. Though all these xml files point to a location with the text name TEXT15. I want to change all the files that containts TEXT15 and change it to TEXT16.

This actually works for files in a folder but not recursively in all the entire files....


perl -pi -c 's/TEXT15/TEXT16/g' ./*


but I have many subfolders and within this more subsub folders....i just want to do this recursively.

sycamorex 07-22-2011 12:49 PM

Try the following:

Code:

#!/bin/bash
for i in $(find . -name *.xml)
do
        sed 's/TEXT15/TEXT16/g' $i
done

Please note that in order to make permanent changes in the files you need to add the -i flag to sed.

grail 07-22-2011 01:21 PM

I am guessing you could probably just use the find itself:
Code:

find . -name *.xml -exec sed 's/TEXT15/TEXT16/g' {} \;


All times are GMT -5. The time now is 07:49 AM.