# Comparing 2 files

**URL:** <https://www.ruby-forum.com/t/comparing-2-files/141515>\
**Category:** Ruby\
**Created:** [June 26, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/comparing-2-files/141515 "2008-06-26T18:07:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Aaron\_Scott](https://www.ruby-forum.com/u/Aaron_Scott)\
**Post date:** [June 26, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/comparing-2-files/141515/1 "2008-06-26T18:07:35Z")

</div>

I’m very new to Ruby. I’m wondering if it is possible to compare 2 text  
files and if those files have any differences in them that I can then  
automatically send myself an email stating that the 2 files are not the  
same. Thanks for any help. I tried using ruby\_diff but I don’t think  
it will work for my purposes.

Aaron

---

<div class="post-metadata">

**Author:** ![Aaron\_Scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Aaron\_Scott](https://www.ruby-forum.com/u/Aaron_Scott)\
**Post date:** [June 26, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/comparing-2-files/141515/2 "2008-06-26T18:26:42Z")

</div>

Aaron Scott wrote:

> I’m very new to Ruby. I’m wondering if it is possible to compare 2 text  
> files and if those files have any differences in them that I can then  
> automatically send myself an email stating that the 2 files are not the  
> same. Thanks for any help. I tried using ruby\_diff but I don’t think  
> it will work for my purposes.
> 
> Aaron

> ri FileUtils#compare\_file  
> ------------------------------------------------- FileUtils#compare\_file  
> compare\_file(a, b)

* * *

```
 Returns true if the contents of a file A and a file B are
 identical.

   FileUtils.compare_file('somefile', 'somefile') #=> true
   FileUtils.compare_file('/bin/cp', '/bin/mv') #=> maybe false

 (also known as identical?, cmp)
```

---

<div class="post-metadata">

**Author:** ![Aaron\_Scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Aaron\_Scott](https://www.ruby-forum.com/u/Aaron_Scott)\
**Post date:** [June 26, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/comparing-2-files/141515/3 "2008-06-26T18:31:26Z")

</div>

Aaron Scott wrote:

> I’m wondering if it is possible to compare 2 text  
> files and if those files have any differences in them that I can then  
> automatically send myself an email stating that the 2 files are not the  
> same.

The first thing you could try is to stat the files (builtin File::Stat  
class) and see if there are any relevant differences between them (e.g.  
size, mtime).

Or you could ask the OS to do a diff on the files.

Or you could read each file into a string and compare the two strings.

Or…

---

<div class="post-metadata">

**Author:** ![Aaron\_Scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@Aaron\_Scott](https://www.ruby-forum.com/u/Aaron_Scott)\
**Post date:** [June 26, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/comparing-2-files/141515/4 "2008-06-26T21:07:52Z")

</div>

Thanks That worked perfectly.

Aaron Scott  
Help Desk Technician  
MedAlliance Management Group, Inc.  
Waterbrooke Professional Park  
2312 Knob Creek Road, Suite 200  
Johnson City, Tennessee 37604  
Phone: 423.915.1126 x 18  
Fax: 423.915.0635  
Web: www.medalliance.md
