# Read from middle/near end of files?

**URL:** <https://www.ruby-forum.com/t/read-from-middle-near-end-of-files/85832>\
**Category:** Rails\
**Created:** [February 16, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/read-from-middle-near-end-of-files/85832 "2007-02-16T17:39:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 16, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/read-from-middle-near-end-of-files/85832/1 "2007-02-16T17:39:45Z")

</div>

Hi,

Does anyone know if it is possible to read say the last 10 line of a  
file? readlines would work if memory is unlimited, but that isn’t the  
case, especially if the file is a 2GB log file. Or is this not possible  
at all?

Thanks.

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 16, 2007, 5:48pm UTC](https://www.ruby-forum.com/t/read-from-middle-near-end-of-files/85832/2 "2007-02-16T17:48:35Z")

</div>

> Does anyone know if it is possible to read say the last 10 line of a  
> file? readlines would work if memory is unlimited, but that isn’t the  
> case, especially if the file is a 2GB log file. Or is this not possible  
> at all?

Never used it, but a google search for ‘ruby file tail’ turns up this:

[http://raa.ruby-lang.org/project/file-tail/](http://raa.ruby-lang.org/project/file-tail/)

You could also get the size of the file, open it, and then seek to  
somewhere near the end and just start reading at that point till you  
reach  
the end…

-philip

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 16, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/read-from-middle-near-end-of-files/85832/3 "2007-02-16T18:29:40Z")

</div>

On Feb 16, 2007, at 11:57 AM, Philip H. wrote:

> You could also get the size of the file, open it, and then seek to  
> somewhere near the end and just start reading at that point till  
> you reach  
> the end…
> 
> -philip

I _have_ used file-tail and taken a look at the code. It seems to be  
well written and covers possible gotchas quite well. For example, to  
get the revision number out of the revisions.log file that capistrano  
adds a line to on each deploy:

```
     rev = File.open(name) do |f|
       f.extend(File::Tail)
       f.rewind(1).gets.match(/(\d+) \d{14}$/)[1]
     end

```

You gotta like that!

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 16, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/read-from-middle-near-end-of-files/85832/4 "2007-02-16T22:21:07Z")

</div>

Thanks a lot. file-tail is exactly what I am looking for.
