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
-type f ??
the string could be a name of file name or subdirectory name
Thanks pons
On Fri, Sep 23, 2011 at 8:51 PM, m.roth@5-cent.us wrote:
On Fri, Sep 23, 2011 at 1:21 PM, m.roth@5-cent.us wrote:
Either way, it's probably a bad idea to have a script that renames directories in mid-path while you are recursing the tree without giving it some thought.
I am planning to have this in 2 stages first -type f then -type d
pons
On Fri, Sep 23, 2011 at 11:15 PM, m.roth@5-cent.us wrote:
On 09/23/11 1:51 PM, madunix@gmail.com wrote:
I am planning to have this in 2 stages first -type f then -type d
you likely should use the -depth option that says descend first, even if you do the files seperately.... if you use -depth, you don't have to do it in two phases.
On 2011-09-23 19:47, madunix@gmail.com wrote:
A 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@gmail.com madunix@gmail.com wrote:
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".
how about...
find . -depth -execdir mv {} ${{}/old/new} ;
I do highly recommend test-running this with a "echo " in front of the mv command. I didn't test it.