Search code examples
bashinotifywait

inotifywait not displaying correct path


I have a script to automate adding movie data to my website and download the correct subtitles for them by using inotify to launch other scripts. So that it only runs on the new file I need the complete file path like "/var/www/html/movies/my movie (2020)/" and file name "my movie (2020).mp4"

I've tried different methods to get it working such as:

inotifywait -m -r --format "%e %w%f" /var/www/html/uploads/Videos/ -e create -e moved_to |
    while read event fullpath; do

Above doesn't pick up any files.

inotifywait -mr --format "%w%f" /var/www/html/uploads/Videos/ -e create -e moved_to |
    while read event fullpath; do

Works for movies like "Freaks (2018)" but not "Zombieland Double Tap (2019)" it becomes "ZombielandTap (2019)"

Without --format at all just completely messes up some directories: 2 fast 2 furious (2003) becomes:

/dir/22 Furious (2003)/ CREATE 2 Fast 2 Furious (2003).mp4

My exact needs for it is to grab the path to and file name (I use both separately). Upon download completion in a non monitored folder the movie is moved and renamed from whatever torrent name to the actual name in the monitored directory. The final file is all I care about having the link for.

What can I do to get the path and file name either separately so I can add them together when I need it (preferred) or full path I can break up with expressions?


Solution

  • You have a couple of issues. In your first attempt, you monitor the directory, but you pipe the output of inotifywait to read. The pipe is not within any loop so it is a one-shot deal. You are left monitoring the directory, but your output is no longer connected to your read loop.

    Your second attempt has the same issue, but this is compounded by using --format "%w%f" that does not output the event, but then attempting to read event fullpath which since your filename contains whitespace, part of the name is read into event and the rest into fullpath.

    Since you are only concerned with files added to the directory --create should be the only event you need to monitor. Your use of %w%f should be fine since you are monitoring a directory, '%w' will contain the watched directory (watched_filename) and '%f' will contain the filename (event_filename).

    You are using bash, so you can use process substitution in bash to establish recursive monitoring of the directory and feed filenames to a while loop to process the changes. You must quote the resulting variables throughout the rest of your script to avoid word-splitting depending on how you use them.

    You can do something similar to:

    while read -r fullpath
    do
        echo "got '$fullpath'"
    done < <(inotifywait -m -r --format '%w%f' -e create "/var/www/html/uploads/Videos/")
    

    This will continually read the absolute path and filename into the fullpath variable. As written the watch simply echos, e.g. "got '/var/www/html/uploads/Videos/name of new file.xxx'"

    Example Use/Output

    With the watch set on a test directory "my dir" with spaces and creating files in that directory (also with spaces) results in, e.g.

    Setting up watches.  Beware: since -r was given, this may take a while!
    Watches established.
    got '/home/david/tmpd/my dir/my movie1 (2020).mp4'
    got '/home/david/tmpd/my dir/my movie2 (2020).mp4'
    got '/home/david/tmpd/my dir/my movie3 (2020).mp4'
    

    You can use fullpath in whatever further processing you need. Give it a try and let me know if you have further questions.