Shankar Menon
Shankar Menon

Reputation: 279

How to capture the event of changing a file in a folder?

Will there be any event generated when the contents of a folder in UNIX is changed(removed, added or edited), and can this event be captured to trigger another script?

Upvotes: 2

Views: 1071

Answers (1)

larsks
larsks

Reputation: 312700

If you're using Linux, you can use the inotify subsystem to catch this sort thing. There are inotify bindings for Python, Perl, and so forth, or you can use the inotifywait program (part of the inotify-tools package) in shell scripts.

For example, if I run inotifywait -m /tmp, this will watch /tmp for changes. If I create a file, I see:

/tmp/ CREATE foo
/tmp/ OPEN foo
/tmp/ ATTRIB foo
/tmp/ CLOSE_WRITE,CLOSE foo

And if I delete that file, I see:

/tmp/ DELETE foo

You can write a shell script that reads these notifications from inotifywait and acts on them accordingly.

Upvotes: 4

Related Questions