Hi I would like to use a bash script that searches files and subdirectories name in a directory /var/ww/html/web for a specific string, and when it finds the search string, replaces the string (old1) with new string (new1), and so on old2 with new2 ....oldn with newn. replace_string.sh #!/bin/bash for db in $(find /var/www/html/web -name * -exec) do sed -e "s/old1/new1/" \ sed -e "s/old2/new2/" \ ... sed -e "s/oldn/newn/" $db done Any recommendation.. Thanks pons
madunix at gmail.com wrote:> > I would like to use a bash script that searches files and > subdirectories name in a directory /var/ww/html/web > for a specific string, and when it finds the search string, replaces > the string (old1) with new string (new1), and so on > old2 with new2 ....oldn with newn. > > replace_string.sh > #!/bin/bash > for db in $(find /var/www/html/web -name * -exec) > do > sed -e "s/old1/new1/" \ > sed -e "s/old2/new2/" \ > ... > sed -e "s/oldn/newn/" $db > doneClose. I think find /var/www/html/web -type f -exec sed -i -e "s/old1/new1/;s/old2/new2/;..." {} \; would do it. The -i is to make the change inplace, editing the existing file. mark
On 2011-09-23 19:47, madunix at gmail.com wrote:> Hi > > > I would like to use a bash script that searches files and > subdirectories name in a directory /var/ww/html/web > for a specific string, and when it finds the search string, replaces > the string (old1) with new string (new1), and so on > old2 with new2 ....oldn with newn. > > > > replace_string.sh > #!/bin/bash > for db in $(find /var/www/html/web -name * -exec) > do > sed -e "s/old1/new1/" \ > sed -e "s/old2/new2/" \ > ... > sed -e "s/oldn/newn/" $dbA more efficient way to perform sed is sed -e "s/old1/new1/" \ -e "s/old2/new2/" \ ... -e "s/oldn/newn/" $db or sed -e "s/old1/new1/ ; s/old2/new2/" .. $db Other hints for efficient nash shell scripts.. http://hacktux.com/bash/script/efficient
On Fri, Sep 23, 2011 at 10:47 AM, madunix at gmail.com <madunix at gmail.com> wrote:> > I would like to use a bash script that searches files and > subdirectories name in a directory /var/ww/html/web > for a specific string, and when it finds the search string, replaces > the string (old1) with new string (new1), and so on > old2 with new2 ....oldn with newn.It would appear from the subsequent discussion that what you're trying to do is rename files and subdirectories, NOT change the CONTENTS of the files. All these suggestions with sed so far are for changing strings in the file contents, and won't work on the names of anything. I think you want to start by simply collecting all the file and subdirectory names: find /var/www/html/web -depth -print (this assumes that none of the file names contains embedded newlines). Next you want to transform those names using your set of patterns, and then rename the results: find /var/www/html/web -depth -print | \ while read oldname do newname="$(echo "$oldname" | sed -e "s/old1/new1/" \ -e "s/old2/new2/" \ -e ... \ -e "s/oldN/newN/")" mv "$oldname" "$newname" done If the names contain leading/trailing spaces or other odd characters then a simple pipe to "while read" probably won't work and you will have to resort to a perl script or the like. Note also you can pass multiple -e options to "sed".