I’m trying to find a way to immediately move a file to another folder as soon as it appears in my dropbox on CentOS.
I have scoured the internet for some clues but I can’t get any further than the fact that I need to use inotify to invoke a script which will process the file as it appears.
My BASH knowledge is very limited and I doubt I can write this in PHP.
In other words, how can I move a file to another folder as soon as it appears using inotify?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The command inotifywait prints a message each time a the contents of the specified directory change. You’ll need to pick on the right event: not when the file is created, but when Dropbox has finished downloading it. I don’t know exactly how Dropbox operates. If it writes to the file in its final place, listen to close_write events. If it writes to the file into a temporary location and then moves it into place, listen to moved_to events. Pass the options -m -q to inotifywait so that it keeps listening for events (by default, it only waits for the next event) and only prints relevant messages.
cd ~/Dropbox/myfolder inotifywait -m -q -e close_write --format %f . | while IFS= read -r file; do cp -p "$file" /path/to/other/directory done
Method 2
This is a simple approach:
#!/usr/bin/env bash
dir=/home/ortix/Dropbox/new/
target=/home/ortix/movedfiles/
inotifywait -m "$dir" --format '%w%f' -e create |
while read file; do
mv "$file" "$target"
done
With more details about the types of files you wanted to move, you could add some checking, logging etc…
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0