# Inotify subdirectories

**URL:** <https://www.ruby-forum.com/t/inotify-subdirectories/83173>\
**Category:** Ruby\
**Created:** [January 24, 2007, 1:50am UTC](https://www.ruby-forum.com/t/inotify-subdirectories/83173 "2007-01-24T01:50:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![j2f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@j2f](https://www.ruby-forum.com/u/j2f)\
**Post date:** [January 24, 2007, 1:50am UTC](https://www.ruby-forum.com/t/inotify-subdirectories/83173/1 "2007-01-24T01:50:47Z")

</div>

i have been able to successfully use the inotify  
([http://raa.ruby-lang.org/project/ruby-inotify/](http://raa.ruby-lang.org/project/ruby-inotify/)) to monitor a directory,  
and monitor subdirectories when they are created.

However, when a file is created in a newly monitored subdirectory, I  
cannot figure out how I can get the full path to that file. to make this  
clear, say i am monitoring the directory “/tmp/monitor”, the script will  
detect that a subdirectory “subdir” is created and will monitor that.  
however, when a file (say “test”) is created under “subdir”, I do not  
know how to get to that file, i.e. how can i get the full path  
“/tmp/monitor/subdir/test”.

i hope that was clear. can anyone help?

(i erroneously posted this message earlier at RForum. sorry for the  
cross-post)

---

<div class="post-metadata">

**Author:** ![j2f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@j2f](https://www.ruby-forum.com/u/j2f)\
**Post date:** [January 24, 2007, 8:26am UTC](https://www.ruby-forum.com/t/inotify-subdirectories/83173/2 "2007-01-24T08:26:40Z")

</div>

On 1/24/07, Jay2 Fernando [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “/tmp/monitor/subdir/test”.  
> You need to maintain a hash of inotify watch descriptors to actual  
> paths. e.g.

require ‘inotify’  
require ‘find’

EVENTS = { 256 =\> “created”, 512 =\> “deleted”, 128 =\> “moved” }  
watches = {}  
i = Inotify.new

Dir[“/home/\*”].each {|userdir|  
begin  
wd = i.add\_watch(userdir, Inotify::ALL\_EVENTS)  
watches[wd] = userdir  
rescue  
puts “Skipping #{userdir}: #{$!}”  
end  
}

t = Thread.new {  
i.each\_event {|ev|  
unless ev.name =~ /^./  
puts “File #{ev.name} #{EVENTS[ev.mask]} in #{watches[ev.wd]}”  
end  
}  
}

t.join

---

<div class="post-metadata">

**Author:** ![j2f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@j2f](https://www.ruby-forum.com/u/j2f)\
**Post date:** [January 24, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/inotify-subdirectories/83173/3 "2007-01-24T14:20:45Z")

</div>

On 1/24/07, Jay2 Fernando [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried this code but I get the following error:
> 
> undefined method `wd’ for \<Inotify::Event:0xDEADBEEF name=FIXME  
> mask=FIXME\>:Inotify::Event (NoMethodError)
> 
> I got the source code from the URL above. I read the inotify header file  
> and saw the “wd” event. what could be the problem?

You’ve got an old version of the inotify code - get it from here:  
[http://nbtsc.org/~aredridel/projects/ruby/ruby-inotify/](http://nbtsc.org/~aredridel/projects/ruby/ruby-inotify/)

martin

---

<div class="post-metadata">

**Author:** ![j2f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@j2f](https://www.ruby-forum.com/u/j2f)\
**Post date:** [January 24, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/inotify-subdirectories/83173/4 "2007-01-24T15:15:49Z")

</div>

Martin DeMello wrote:

> On 1/24/07, Jay2 Fernando [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I tried this code but I get the following error:
> > 
> > undefined method `wd’ for \<Inotify::Event:0xDEADBEEF name=FIXME  
> > mask=FIXME\>:Inotify::Event (NoMethodError)
> > 
> > I got the source code from the URL above. I read the inotify header file  
> > and saw the “wd” event. what could be the problem?
> 
> You’ve got an old version of the inotify code - get it from here:  
> [http://nbtsc.org/~aredridel/projects/ruby/ruby-inotify/](http://nbtsc.org/~aredridel/projects/ruby/ruby-inotify/)
> 
> martin

aha… ok. thanks for pointing me to correct source.

---

<div class="post-metadata">

**Author:** ![j2f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@j2f](https://www.ruby-forum.com/u/j2f)\
**Post date:** [January 24, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/inotify-subdirectories/83173/5 "2007-01-24T14:04:37Z")

</div>

Martin DeMello wrote:

> On 1/24/07, Jay2 Fernando [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > “/tmp/monitor/subdir/test”.  
> > You need to maintain a hash of inotify watch descriptors to actual  
> > paths. e.g.
> 
> require ‘inotify’  
> require ‘find’
> 
> EVENTS = { 256 =\> “created”, 512 =\> “deleted”, 128 =\> “moved” }  
> watches = {}  
> i = Inotify.new
> 
> Dir[“/home/\*”].each {|userdir|  
> begin  
> wd = i.add\_watch(userdir, Inotify::ALL\_EVENTS)  
> watches[wd] = userdir  
> rescue  
> puts “Skipping #{userdir}: #{$!}”  
> end  
> }
> 
> t = Thread.new {  
> i.each\_event {|ev|  
> unless ev.name =~ /^./  
> puts “File #{ev.name} #{EVENTS[ev.mask]} in #{watches[ev.wd]}”  
> end  
> }  
> }
> 
> t.join

I tried this code but I get the following error:

undefined method `wd’ for \<Inotify::Event:0xDEADBEEF name=FIXME  
mask=FIXME\>:Inotify::Event (NoMethodError)

I got the source code from the URL above. I read the inotify header file  
and saw the “wd” event. what could be the problem?
