开发者

Unix invoke script when file is moved

开发者 https://www.devze.com 2023-01-05 06:41 出处:网络
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 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 m开发者_JAVA技巧v 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号