Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor a complete directory tree for changes in Linux?

How can I monitor a whole directory tree for changes in Linux (ext3 file system)?

Currently the directory contains about half a million files in about 3,000 subdirectories, organized in three directory levels.

Those are mostly small files (< 1kb, some few up to 100 kb). It's a sort of queue and I need to know when files are being created, deleted or their content modified within 5-10 seconds of that happening.

I know there is inotify and sorts, but AFAIK they only monitor a single directory, which means I would need 3,000 inotify handles in my case - more than the usual 1024 handles allowed for a single process. Or am I wrong?

In case the Linux system can't tell me what I need: perhaps there is a FUSE project that simulates a file system (replicating all file accesses on a real file system) and separately logs all modifications (couldn't fine one)?

like image 877
Udo G Avatar asked Jan 02 '12 09:01

Udo G


People also ask

How do I monitor changes in Linux?

In Linux, the default monitor is inotify. By default, fswatch will keep monitoring the file changes until you manually stop it by invoking CTRL+C keys. This command will exit just after the first set of events is received. fswatch will monitor changes in all files/folders in the specified path.

How do I see the tree structure of a directory in Linux?

You need to use command called tree. It will list contents of directories in a tree-like format. It is a recursive directory listing program that produces a depth indented listing of files. When directory arguments are given, tree lists all the files and/or directories found in the given directories each in turn.


1 Answers

I've done something similar using the inotifywait tool:

#!/bin/bash while true; do  inotifywait -e modify,create,delete -r /path/to/your/dir && \ <some command to execute when a file event is recorded>  done 

This will setup recursive directory watches on the entire tree and allow you to execute a command when something changes. If you just want to view the changes, you can add the -m flag to put it into monitor mode.

like image 119
Jason Miesionczek Avatar answered Oct 09 '22 03:10

Jason Miesionczek