Operating System - HP-UX
1844100 Members
2754 Online
110227 Solutions
New Discussion

Replace the word in directories and sub-directories

 
Qcheck
Super Advisor

Replace the word in directories and sub-directories

Want to replace test with \\Test for all the files in the folder and subfolders.

Please advise.
9 REPLIES 9
Prasanth Vattoly
Frequent Advisor

Re: Replace the word in directories and sub-directories


You can use sed command for this
prasanth.v.a
Steven E. Protter
Exalted Contributor

Re: Replace the word in directories and sub-directories

Shalom,

The tool for this is sed

"sed s/indexnfweekly.html/index2.html/g ${filetoback} > $filetoback.bck"

What that little command does is change where it says indexnfweekly.html to a index2.html

find /directory -exec grep -l "yourtext" {} ;\ >list

while read -r oldfile
do
sed $oldfile > $oldfile.new > newfilename
mv $oldfile $newfile
done < list


SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Peter Nikitka
Honored Contributor

Re: Replace the word in directories and sub-directories

Hi,

it is not clear if this renaming should apply to filenames or the contents of each file - please clarify.

- running such a command - whether file- or stringrenaming is performed will be desastrous in system directories
- what about binary files?

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Qcheck
Super Advisor

Re: Replace the word in directories and sub-directories

Peter,

Renaming should occur with the word test with \\Test within the files in a directory and subdirectories.

Thanks
Qcheck
Super Advisor

Re: Replace the word in directories and sub-directories

Hi steve,

Thanks for the reply. Can I use the script as follows:

#!/bin/ksh
find /webdata/dev/ir/classes -exec grep -l "System" {} ;\ >list
oldfile=System
newfile=\\System
while read -r oldfile
do
sed $oldfile > $oldfile.new > newfilename
mv $oldfile $newfile
done < list

--------------------------

I want to replace System with \\System under /webdata/dev/ir/classes, where it has six directories in which the files which I want to change.

I really appreciate if you can help me out.

Thanks
Qcheck
Super Advisor

Re: Replace the word in directories and sub-directories

Steven!

Erroring out at the following command:
find /webdata/dev/ir/classes -exec grep -| "System" {} ;\ >list
James R. Ferguson
Acclaimed Contributor

Re: Replace the word in directories and sub-directories

Hi:

You have *tranposed" the "\" and the ";". The syntax is:

# find /webdata/dev/ir/classes -exec grep -l "System" {} \; >list

Now, haveing said that, you *need* to limit your selection to only files!

# find /webdata/dev/ir/classes -type f -exec grep -l "System" {} \;\ >list

*AND* you can improve the performance by using a "+" in lieu of a ";"

# find /webdata/dev/ir/classes -type f -exec grep -l "System" {} \+ >list

Of course, non of the above address the presence of any binary files if they are potentially present.

Lastly, be advised that *every* file in your directory will be updated whether or not a substitution was actually needed.

Regards!

...JRF...

Qcheck
Super Advisor

Re: Replace the word in directories and sub-directories

Thank you James, but didn't work, erroring out at find command still while running replace-word.ksh:

#!/bin/ksh
#find /webdata/dev/ir/classes -exec grep -l "System" {} ;\ >list
find /webdata/dev/ir/classes -type f -exec grep -l "System" {} \;\ >list
oldfile=System
newfile=\\System
while read -r oldfile
do
sed $oldfile > $oldfile.new > newfilename
mv $oldfile $newfile
done < list

-------------------
# ./replace-word.ksh
find: 0652-018 An expression term lacks a required parameter.
./replace-word.ksh[4]: system: not found.

#ls /webdata/dev/ir/classes
bean
db
exception
load
report
tag
util

All the above subdirectories in /webdata/dev/ir/classes have files which should be replaced from System to \\System.

Please help me out!


James R. Ferguson
Acclaimed Contributor

Re: Replace the word in directories and sub-directories

Hi:

You wrote "\;\" instead of "\;", For efficiency, I also suggested you use "\+".

There's more wrong with your entire syntax anyway. I think this is what you want:

cat ./filter
#!/ust/bin/sh
find webdata/dev/ir/classes -type f -exec grep -l "System" {} \; > list
while read -r oldfile
do
sed -e 's/System/\\\\System/g' ${oldfile} > ${oldfile}.new
mv ${oldfile}.new ${oldfile}
done < list

...NOTE that your post suggested that you want a double backslash. Hence, the syntax I used will give you that.

Regards!

...JRF...