Results 1 to 10 of 10

Thread: Bash script for monitoring changes in a directory tree

  1. #1
    Join Date
    Jun 2006
    Beans
    54
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Bash script for monitoring changes in a directory tree

    hi all!

    I am trying to write a script and I am pretty new at this. So I think I will need some suggestions and advice.

    I want to write a very small, fast and lightweight script to monitor a directory tree (including sub directories and files) for changes. This could be new files, files being deleted or changes to a file. Changes in this directory will have to trigger another script to run.

    Ways I have found of doing this are by using ls, find or rsync to periodically check for changes while running in a loop

    What would be the most efficient way of doing this? I would be grateful if you could maybe write a little bit of example code.

    Thanks.
    Ubuntu user # 14835

  2. #2
    Join Date
    Dec 2006
    Beans
    1,482
    Distro
    Ubuntu Jaunty Jackalope (testing)

    Re: Bash script for monitoring changes in a directory tree

    You might consider creating a script which takes some sort of snapshot of a directory/tree and writes it to a file and compares the current tree to the previously recorded tree. Then maybe use a cron job to call your script periodically. cron is designed to periodically run tasks without your input, so it might be just the right choice.
    Sharing Wifi Is Caring Wifi {o,o} Ubuntu Community Docs..
    |)__)
    -”-”-
    O RLY?

  3. #3
    Join Date
    Feb 2006
    Location
    Vancouver, BC, Canada
    Beans
    318

    Re: Bash script for monitoring changes in a directory tree

    A more elegant approach than trying to maintain the dir tree state within your script and polling, is to write your script based on one of the tools that wraps the inotify api.

    Inotify can send a message when the dir tree is modified in some way. The inotify-tools package provides a lightweight wrapper around the inotify api that is designed to be used in scripting.

    Here's an example script from the inotifywait man page:

    Code:
          #!/bin/sh
           while inotifywait -e modify /var/log/messages; do
             if tail -n1 /var/log/messages | grep httpd; then
               kdialog --msgbox "Apache needs love!"
             fi
           done

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Bash script for monitoring changes in a directory tree

    for systems with no such tools as inotify, its very easy to create . here's a poor man's way
    Code:
    base="/tmp/monitor.base"
    dir_to_monitor="/home/yhlee/test/"
    check="/tmp/status_now"
    if [ ! -e $base ] ;then
        find "$dir_to_monitor" |sort > $base
    fi
    # check 
    find $dir_to_monitor | sort > $check
    #if there's difference, then there's change.
    diff $base $check 
    ...
    ...
    #more code
    ...

  5. #5
    Join Date
    Jun 2006
    Beans
    54
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Re: Bash script for monitoring changes in a directory tree

    thanks for your replies.

    inotify seems to be the best option. However I have one problem. I've been trying to figure this out but I'm kind of stumped. This is the command I would like to run...

    Code:
    inotifywait -m -r --format '%f' -e modify -e move -e create -e delete ~/test
    This would give me the names of files the are modified, moved, deleted or created in the test directory.

    My problem is: How do I get the output of this command (name of the file) as it is being output... and use it in another script... or in this script in another command?

    thanks.
    Ubuntu user # 14835

  6. #6
    Join Date
    Jun 2006
    Beans
    54
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Re: Bash script for monitoring changes in a directory tree

    it one of those times i find the answer just after asking the question... this is what i was looking for

    Code:
    inotifywait -m -r --format '%f' -e modify -e move -e create -e delete ~/test | while read line
    do
    	echo "hello $line"
    done
    Ubuntu user # 14835

  7. #7
    Join Date
    Feb 2008
    Location
    /home/
    Beans
    379
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash script for monitoring changes in a directory tree

    Not to take over the thread but is there a way to run a process that does this, because there are a lot of processes on my machine that just sit and wait, does this make my comp. go slower
    If there's one thing worse than a program that doesn't work when it should, it's a program that does work when it shouldn't.

    Bob Archer

  8. #8
    Join Date
    Sep 2007
    Location
    Ontario
    Beans
    30
    Distro
    Xubuntu 9.04 Jaunty Jackalope

    Re: Bash script for monitoring changes in a directory tree

    Know how I would implement inotify to do bi-directional sync of directories?

  9. #9
    Join Date
    Jan 2011
    Beans
    30

    Re: Bash script for monitoring changes in a directory tree

    librano proposed on October 30th, 2008 07:32 PM the solution you see. I am unclear where the actual list of changes is being written to. Is it to the screen? Is it to a file?
    Any suggestions on what to add to the script he proposed which would push the results to a file?

  10. #10
    Join Date
    Jan 2012
    Beans
    342

    Re: Bash script for monitoring changes in a directory tree

    Quote Originally Posted by F35 View Post
    [..] I am unclear where the actual list of changes is being written to. Is it to the screen? Is it to a file? Any suggestions on what to add to the script he proposed which would push the results to a file?
    echo prints to stdout .. so redirect to a file like so:

    Code:
    echo $line >> /path/to/logfile.txt
    HTH ... khay

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •