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.

  • 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 Jan 25 '23 at 13:38

2 Answers2

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.

karel
  • 114,770
muru
  • 197,895
  • 55
  • 485
  • 740
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
muru
  • 197,895
  • 55
  • 485
  • 740
Selly
  • 201