16

I can monitor the content changes of a file using tail -f command

Is there a similar way of monitoring the changes of a directory structure the way tail -f monitors file?

I have a long running process that adds file to a certain path under a directory and i want to track the file incomings as it(or they) write(s) to the directories and sub directories.

25

The inotify kernel system is what you need.

  1. Install inotify-tools:

    sudo apt-get install inotify-tools
    
  2. Set up a watch:

    inotifywait /path/to/directory --recursive --monitor
    
  3. Sit back and watch the output.


From man inotifywait:

-m, --monitor
   Instead of exiting  after  receiving  a  single  event,  execute
   indefinitely.   The default behaviour is to exit after the first
   event occurs.
-r, --recursive
   Watch all subdirectories of any directories passed as arguments.
   Watches will be set up recursively to an unlimited depth.   Sym‐
   bolic  links  are  not  traversed.  Newly created subdirectories
   will also be watched.

You can use the --event option to watch for specific events, like creation, modification, etc.

  • 1
    incase any one hit this problem: Failed to watch .; upper limit on inotify watches reached go here blog.sorah.jp/2012/01/24/inotify-limitation – johan.i.zahri Oct 25 '14 at 6:26
  • 1
    On one of my servers I discovered it is no longer called inotify-wait its just inotifywait. They must've changed the name to remove the hyphen. – Jamesking56 Oct 16 '15 at 9:02
  • @Jamesking56 I edited out the hyphen. – muru Oct 16 '15 at 9:06
  • Note, the help for inotifywait says it expects the options before the file paths. – Felix Dombek Aug 21 at 16:09
6

--events is not the filter, you have to use --event. For example, here's the command line for monitoring create/modify events:

# inotifywait . --recursive --monitor --event CREATE --event MODIFY

Then I see:

Setting up watches.  Beware: since -r was given, this may take a while!

And here's the format of the feed:

[path] [event] [file]

e.g.

./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal
./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal
./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.