Replace a text in base directory

how can replace a text from all files in
“/home/user” directory and sub directories (tree)…

Pokkai D. wrote:

how can replace a text from all files in
“/home/user” directory and sub directories (tree)…

Try this:

Dir.glob(’/Users/me/2testing/dir2/**/*’) do |fname|
next if File.directory?(fname)

File.open(“temp.txt”, “w”) do |temp_file|
IO.foreach(fname) do |line| #fname is from outer loop
new_line = line.gsub(“hello world”, “goodbye”)
temp_file.print(new_line)
end

File.delete(fname)
File.rename("temp.txt", fname)

end
end

** means to look in the current directory and all subdirectories for
the filenames *, where * means all file names(including directory
names).

fine
thanks 7stud

On Oct 5, 7:33 am, 7stud – [email protected] wrote:

IO.foreach(fname) do |line|  #fname is from outer loop
  new_line = line.gsub("hello world", "goodbye")
  temp_file.print(new_line)
end

File.delete(fname)
File.rename("temp.txt", fname)

end
end

This botched code bombs. Don’t ever post untest code without
stating that it is untested code.

On Oct 5, 2007, at 10:33 AM, 7stud – wrote:

What you want a EULA(gy)?

William J. wrote:

This botched code bombs. Don’t ever post untest code without
stating that it is untested code.

lol.

William J. wrote:

This botched code bombs. Don’t ever post untest code without
stating that it is untested code.

My god… you ran code posted to a mailing list blindly?!?
I made that mistake… once.

~Wayne

Pokkai D. wrote:

how can replace a text from all files in
“/home/user” directory and sub directories (tree)…

Using a little UNIX magic, you can get away with a shorter solution:

find /home/user -type f -print0 | xargs -0
ruby -pe ‘gsub /pattern/, “replacement”’ -i.bak

The “-i” will modify the file in-place and the “.bak” after it will make
a backup of the original file with a “.bak” suffix.

You could also use sed, awk, perl, etc. in place of ‘ruby’ in the same
command.

Wayne E. Seguin wrote:

William J. wrote:

This botched code bombs. Don’t ever post untest code without
stating that it is untested code.

My god… you ran code posted to a mailing list blindly?!?
I made that mistake… once.

~Wayne

puts “Whiner!”