You are not logged in.

#1 2017-01-14 23:00:50

stabwound
Member
Registered: 2015-07-24
Posts: 48

Any help with this inotifywait bash script?

I'm trying to setup an inotifywait script to do this:

Step 1: Directory is created, files are placed in it, which triggers inotifywatch to fire which does step 2
Step 2: After 30 minutes, the directory moved and a symlink is created back to the old root directory

This is what I have so far but it's not quite working. It's kind of hacked together from different examples I've found online.

inotify.sh:

#!/bin/sh

cd ~/automove/old/
inotifywait -m -e close_write --format %w%f . | while IFS= read -r file; do
  echo $file
  sleep 15
  sh ~/automove/move.sh "$file" ~/automove/new/
done

move.sh:

#!/bin/sh
original="$1" target="$2"
if [ -d "$target" ]; then
  target="$target/${original##*/}"
fi
mv -- "$original" "$target"
ln -s -- "$target" "$original"

My problems with this are the following:

-Directories that have 'quotes' get mangled.
-It seems as though inotifywait will get trigged on the new symlinks, creating an endless loop that goes crazy.
-If a directory triggers the inotifywait and then it sleeps for 15 seconds (which I set as a test, in practice I want 30 minutes) and then another directory shows up before the original 15 seconds is up, it won't even start the second 15 second sleep until the first one is done.

I need the sleep to trigger separately for each inotifywait match.

Can anyone tell me how I can modify this to work the way I need?

Offline

Board footer

Powered by FluxBB