Unfortunately, the inotify
API can not be used to monitor remote file systems.
From man 7 inotify
:
Inotify reports only events that a user-space program triggers through the filesystem API. As a result, it does not catch remote events that occur on network filesystems. (Applications must fall back to polling the filesystem to catch such events.)
Relating questions:
That said, the script in your question would not work as expected anyway.
The idea of using "%w%f"
as the format of inotifywait
's output is to provide the command in the loop with the full path of files that triggered a listened-for file system event. %w
expands to the path of the watched file (the $dir
directory), and %f
expands to the (base)name of the file that caused the event.
Thus, inotifywait
is only emitting a single full path for every event it catches. On the other hand, your read
command is given three variables to fill in: path
, action
and file
. By default, read
splits a read line based on the characters in IFS
and assigns the resulting tokens to the names it is given as arguments: the first token to the first name, the second token to the second name, etc. (And, if there are more tokens than names after the penultimate name, all the remaining tokens are assigned to the last name).
As you can easily check, in your code the full path of any file that trigger a watched-for event is assigned to path
(unless it contained blank characters):
$ inotifywait -m --format "%w%f" -e create -e moved_to /tmp/test |
while read path action file; do
printf 'path: "%s"; action: "%s"; file: "%s"\n' "$path" "$action" "$file"
done
# Type "touch /tmp/test/foo" in a different terminal
path: "/tmp/test/foo"; action: ""; file: ""
Also, as pointed out in a different answer in the Q/A you inked to, you should listen for close_write
events, and not for create
. What you are probably looking for is:
inotifywait -m -q --format "%w%f" -e close_write -e moved_to -- "$dir" |
while IFS= read -r src; do
if [ "${src##*.}" = 'txt' ]; then
mv -- "$src" "$target"
fi
done
-r
tells read
not to interpret backslash-escaped sequences. IFS=
is used to prevent read
from trimming blank characters from the end of file names (to handle the unlikely case of names ending with spaces or tabs).
Note that this will still fail for files whose name contains newline characters.
dir
with this guide: https://wiki.archlinux.org/index.php/CurlFtpFS and work well, but i dont know if becouse it is a mount point not have event in local file system... – Jul 30 '20 at 19:55