# Replace a text in base directory

**URL:** <https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427>\
**Category:** Ruby\
**Created:** [October 5, 2007, 11:48am UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427 "2007-10-05T11:48:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 5, 2007, 11:48am UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/1 "2007-10-05T11:48:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 5, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/2 "2007-10-05T14:33:05Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 5, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/3 "2007-10-05T15:49:39Z")

</div>

fine  
thanks 7stud

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 5, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/4 "2007-10-05T17:00:41Z")

</div>

On Oct 5, 7:33 am, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 6, 2007, 1:02am UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/5 "2007-10-06T01:02:25Z")

</div>

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

> 

What you want a EULA(gy)?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 5, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/6 "2007-10-05T17:33:01Z")

</div>

William J. wrote:

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

lol.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 6, 2007, 3:07am UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/7 "2007-10-06T03:07:31Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 6, 2007, 5:08am UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/8 "2007-10-06T05:08:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 6, 2007, 4:28am UTC](https://www.ruby-forum.com/t/replace-a-text-in-base-directory/116427/9 "2007-10-06T04:28:51Z")

</div>

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!”
