3

Does someone have a shell script or utility that can monitor a specific directory and see which file is growing within a certain time period and possibly by how much?

For example, if I have the following files in a directory:

myfile1.txt
anotherfile.gz
thisonetoo.tar

The script could be run once, and then on the next run time it might say something like

myfile1.txt +5MB
anotherfile.gz -10MB
thisonetoo.tar +100MB
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
crickeys
  • 3,075
  • 3
  • 26
  • 27

1 Answers1

2

I have a good way using inotifywait & :

cd /path/to/dir
inotifywait -m -e modify  -r . |
while read a; do
    [[ $a =~ MODIFY[[:space:]]+(myfile1.txt|anotherfile.gz|thisonetoo.tar) ]] &&
    du -h "${BASH_REMATCH[1]}"
done

Note

This have the advantage to avoid polling every N seconds.

inotify is an inode-based filesystem notification technology. It provides possibility to simply monitor various events on files in filesystems. It is a very much powerful replacement of (obsolete) dnotify. inotify brings a comfortable way how to manage files used in your applications.

See inotify doc

(It's just a start, you need a bit more code to have the desired output, but this will be easy now)

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • please note that this approach is limited to a small amount of files. it is not feasable for many hundreds more files. – devsnd Mar 14 '13 at 23:15
  • @twall Why ? Any related links ? Can you define precisely "a small amount of files" ? – Gilles Quénot Mar 14 '13 at 23:19
  • 1
    see: http://stackoverflow.com/questions/11110245/inotify-fd-why-is-the-limit-per-user-id-and-not-per-process so the limit is set in /proc/sys/fs/inotify/max_user_instances and might not be changable depending on your setup (root needed) – devsnd Mar 15 '13 at 10:41
  • I ended up using a PHP script for this as my system didn't seem to support inotify. But, kudos to the best answer. – crickeys Mar 15 '13 at 18:37