I'm trying to use inotifywait to watch a download directory and copy new files to another directory. It isn't working as expected. What should I check?
Why isn't my inotify command watching the directory?
Answers
Check the path first. A tilde inside double quotes doesn't expand to your home directory, so use "$HOME/data/downloads" instead of "~/data/downloads".
Print the source variable and check that the directory exists. Also look for repeated path components if you're getting a directory not found error.
Reply to this answer
Get the watcher working on its own before adding the copy step. For example, try inotifywait -mr -e close_write -e moved_to "$SOURCE" --format '%w%f' and see what it prints when a file is written or moved into the watched tree.
The format joins the watched directory path and filename, giving you a path you can pass to another command. Recursive watching is enabled by -r. Once the output looks right, you can pipe it into a loop that reads each path and copies it.
not__a__blueprint:
Does the loop variable need to be set before read runs?
__temporary_foreman:
No. In while read -r path, read assigns each line to path. If you use read -r without naming a variable, it assigns the line to REPLY instead.
Reply to this answer
Double check the order of the arguments to cp. The event path is the source and your ingest directory is the destination, so the loop would use cp -- "$path" "$DEST/". Quote both variables so spaces in paths don't split them into separate arguments.
Copying everything into one directory can overwrite files with the same name. If you need to retain the directory structure, look at the cp --parents option and test it with a small directory first.
Reply to this answer
A create event can happen before a file has finished being written. For a copy workflow, close_write is a useful event to consider for completed writes, while moved_to can catch files moved into the watched directory. Test both against the way files actually arrive.
Reply to this answer
Add your answer
Answers are reviewed before publication.
decimal_508~approx:
If the directory is inside a container, check the path as seen by the process running inotifywait, not just the path on the host.