23

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.

1
  • It sounds like you only care if the listing of the directory changes. If a file changes, you don't need the command to run. Is that correct?
    – Flimm
    Commented Jan 25, 2023 at 13:38

2 Answers 2

32

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.

4
  • 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 Commented Oct 25, 2014 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. Commented Oct 16, 2015 at 9:02
  • @Jamesking56 I edited out the hyphen.
    – muru
    Commented Oct 16, 2015 at 9:06
  • Note, the help for inotifywait says it expects the options before the file paths. Commented Aug 21, 2019 at 16:09
8

--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
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .