linux – Monitor directory listing for changes? – Education Career Blog

On a unix system, how do I monitor (like how ‘tail’ works) a directory for changes made to files – either new ones created, or size changes, etc.

Looking for a command line tool rather than something to be installed.

,

Most unix variants have an API for this, but it’s not standardized. On Linux, there is inotify. On the command line, you can use inotifywait. Usage example:

inotifywait -m /path/to/dir | while read -r dir event name; do
  case $event in
    OPEN) echo "The file $name was created or opened (not necessarily for writing)";;
    WRITE) echo "The file $name was written to";;
    DELETE) echo "The file $name was deleted ";;
  esac
done

Inotify event types are often not exactly what you’re trying to notice (e.g. OPEN is very wide), so don’t feel bad if you end up making your own file checks.

,

you can craft your own then if you don’t want to install tools. Just an idea.
Create a base line file of your directory using find command. Use a loop or cron job, find the directory using the same parameters, and check the new file against the base line file. Use a tool like diff to get the differences..

eg

find /path other options >> baseline.txt 
while true #or use a cron job
do
  find /path same options >> listing.txt
  diff baseline.txt listing.txt
  # do processing here...
  mv listing.txt baseline.txt  # update the baseline.
  sleep 60
done

Leave a Comment