views:

37

answers:

2

Hi. I have tons of files dumped into a few different folders. I've tried organizing them several times, unfortunatly, there is no organization structure that consistently makes sense for all of them.

I finally decided to write myself an application that I can add tags to files with, then the organization can be custom to the actual organizational structure.

I want to prevent from getting orphaned data. If I move/rename a file, my tag application should be told about it so it can update the name in the database. I don't want it tagging files that no longer exist, and having to readd tags for files that used to exist.

Is there a way I can write a callback that will hook into the mv command so that if I rename or move my files, they will invoke the script, which will notify my app, which can update its database?

My app is written in Ruby, but I am willing to play with C if necessary.

A: 

Can you control the path of your users? Place a script or exe and have the path point to it before the standard mv command. Have this script do what you require and then call the standard mv to perform the move.

Alternately an alias in each users profile. Have the alias call your replacement mv command.

Or rename the existing mv command and place a replacement in the same dir, call it mv and have it call your newly renamed mv command after doing what you want.

Karl
This does not catch the case when some other application (like a file manager) is moving a file, as those usually don't use the shell command to do that.
Fabian
Of course. And it shows my blind spot that I never considered a GUI on a UNIX system :) After all once you've got a command line what else could you ever want ?
Karl
+2  A: 

If you use Linux you can use inotify (manpage) to monitor directories for file events. It seems there is a ruby interface for inotify.

From the Wikipedia:

Some of the events that can be monitored for are:
IN_ACCESS - read of the file
IN_MODIFY - last modification
IN_ATTRIB - attributes of file change
IN_OPEN and IN_CLOSE - open or close of file
IN_MOVED_FROM and IN_MOVED_TO - when the file is moved or renamed
IN_DELETE - a file/directory deleted
IN_CREATE - a file in a watched directory is created
IN_DELETE_SELF - file monitored is deleted

This does not work for Windows (and I think also not for other Unices besides Linux) as inotify does not exist there.

Fabian
That is exactly what I am looking for. But I'm on a Mac. Using that hint, I found FSEvents http://en.wikipedia.org/wiki/FSEvents which looks like it should do the same thing. Found two gems for it, the one named after the lib hasn't been touched in 2 years, the other is 6mo old. I'll try them out this week and update the question, thanks.
Joshua Cheek
Well, looks like no dice. The newer gem just uses on the older gem. The older gem uses RubyCocoa, which you apparently have to install with a .pkg Since I have many versions of Ruby, I think it doesn't know which one to install for, or something, because I still can't require it. I'll keep looking and playing, thanks for this tip.
Joshua Cheek