[GTALUG] Watching a network folder: is there a smart way of doing this?

Scott Elcomb psema4 at gmail.com
Fri Feb 24 23:21:09 EST 2017


On Fri, Feb 24, 2017 at 9:24 PM, Stewart C. Russell via talk
<talk at gtalug.org> wrote:
> On 2017-02-20 12:47 AM, Aruna Hewapathirane wrote:
>>
>> Hi Stewart, you could just |watch| the file listing (adjusting n seconds
>> to whatever is suitable)
>>
>> |watch --differences -n 10 ls -l </path/to/shared/dir>|
>
> I hadn't heard of watch before, so thanks! watch *started* to work
> really well, but then went into a terminal sulk after the FS disappeared
> during a scan, and refused to show any updates. It's also an interactive
> program, so doesn't pipe or notify changes in any useful way.
>
> I suspect I'll just have to go with William Park's suggestion of using
> rsync to a local folder that I have more control over. I still have to
> correct for the scanner FS's wandering clock, but that's less important.

Here's a quick and dirty bash script similar to the watch command;
adjust to taste. :-)

[- watchdir.sh snippet starts -]

#!/usr/bin/env bash
#
# USAGE
# watchdir.sh <rate> <path>
#


# Determine our sampling rate
WATCHRATE=${1}

if [[ "" = "${WATCHRATE}" ]]; then
    WATCHRATE=10
fi

# Determine our watch directory
WATCHDIR=${2}

if [[ "" = "${WATCHDIR}" ]]; then
    WATCHDIR="."
fi

# Describe what we're doing
echo "Watching ${WATCHDIR} every ${WATCHRATE}s:"

# Render a side-by-side comparison with last snapshot of our directory
# note: assumes at least one snapshot already exists
function sample_watchdir {
    cp .current .previous
    date > .current
    ls -l ${WATCHDIR} >> .current
    echo ""
    diff -y .current .previous
}

# Begin watching
date > .current
ls -l ${WATCHDIR} >> .current
sample_watchdir

# Every WATCHRATE seconds, make the current file listing our previous
#   listing, take a fresh look at WATCHDIR and compare the changes
while sleep ${WATCHRATE}; do
    sample_watchdir
done

[- watchdir.sh snippet ends -]

-- 
  Scott Elcomb         @psema4
     http://www.pirateparty.ca/


More information about the talk mailing list