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@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 done
Close. 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
-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:
madunix@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 done
Close. 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
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
madunix@gmail.com wrote:
-type f ??
the string could be a name of file name or subdirectory name
No, it can't. You are *not* going to edit directory names this way, and should not.
mark
Thanks pons
On Fri, Sep 23, 2011 at 8:51 PM, m.roth@5-cent.us wrote:
madunix@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 done
Close. 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
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
madunix@gmail.com wrote:
-type f ??
the string could be a name of file name or subdirectory name
I hate webmail. After I hit <send> and while it was thinking about going, I realized another question: are you trying to rename files?
mark
Thanks pons
On Fri, Sep 23, 2011 at 8:51 PM, m.roth@5-cent.us wrote:
madunix@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 done
Close. 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
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
yes files and directories too ..
pons
On Fri, Sep 23, 2011 at 9:08 PM, m.roth@5-cent.us wrote:
madunix@gmail.com wrote:
-type f ??
the string could be a name of file name or subdirectory name
I hate webmail. After I hit <send> and while it was thinking about going, I realized another question: are you trying to rename files?
mark
Thanks pons
On Fri, Sep 23, 2011 at 8:51 PM, m.roth@5-cent.us wrote:
madunix@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 done
Close. 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
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
madunix@gmail.com wrote:
pons
On Fri, Sep 23, 2011 at 9:08 PM, m.roth@5-cent.us wrote:
madunix@gmail.com wrote:
-type f ??
the string could be a name of file name or subdirectory name
<snip>
I realized another question: are you trying to rename files?
yes files and directories too ..
Then different commands - sed for the files, mv for the directories.
This begins to look like a perl script.
mark
On Fri, Sep 23, 2011 at 8:51 PM, Â m.roth@5-cent.us wrote:
madunix@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 done
Close. 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
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
On Fri, Sep 23, 2011 at 1:21 PM, m.roth@5-cent.us wrote:
I realized another question: are you trying to rename files?
yes files and directories too ..
Then different commands - sed for the files, mv for the directories.
This begins to look like a perl script.
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.
Les Mikesell wrote:
On Fri, Sep 23, 2011 at 1:21 PM, m.roth@5-cent.us wrote:
I realized another question: are you trying to rename files?
yes files and directories too ..
Then different commands - sed for the files, mv for the directories.
This begins to look like a perl script.
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.
Urk! Good catch, Les, I hadn't even *thought* of that.
mark
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:
Les Mikesell wrote:
On Fri, Sep 23, 2011 at 1:21 PM, m.roth@5-cent.us wrote:
I realized another question: are you trying to rename files?
yes files and directories too ..
Then different commands - sed for the files, mv for the directories.
This begins to look like a perl script.
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.
Urk! Good catch, Les, I hadn't even *thought* of that.
mark
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
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:
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
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:
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".
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.