[CentOS] replace multiple string
Bart Schaefer
barton.schaefer at gmail.com
Fri Sep 23 21:58:07 UTC 2011
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".
More information about the CentOS
mailing list