I am trying to make a copy of completed files that are saved to a watch folder. I have created a bash script using inotifywait to watch the folder.
My original script worked great with files and folders/subfolders. Once I replaced the "-e create -e moved_to" with "-e close_write" it no longer recognizes/processes folders that are saved in the watch folder. It only works now with files. The reason I am needing to change to this is because a lot of my files are large (and multiples in folders) and takes some time to be saved completely (to the watch folder) and the script runs through all the processes before the files are completely saved/copied.
I have also tried the "wait" command but it does nothing at all. I know there are more complicated/better/correct ways of using it but I have not figured them out yet. After finding the "close_write" event in inotify I felt it would do exactly what I am needing by waiting to for the current process/command to finish before starting the next. And it works perfectly on large single files but is MIA (does nothing) whenever a folder is copied to the watch folder.
Here is a sample of my script. I am not well versed in writing bash scripts so sorry if it is a mess.
#!/bin/bash WATCHED=/mnt/Watched DESTINATION=/mnt/Destination user=userid group=groupid perms=777 # Step 1 - Copying completed files to Destination folder inotifywait -m -e close_write --format %f $WATCHED \ | while read new do echo Detected new file $new, Copying to Destination folder cp -r "$WATCHED/$new" "$DESTINATION/" wait # Step 2 - Deleting unwanted files from Destination folder echo Deleting unwanted files from Destination folder find $DESTINATION -type f -iname "*.txt" -delete find $DESTINATION -type f -iname "*.nfo" -delete find $DESTINATION -type f -iname "*.website" -delete find $DESTINATION -type f -iname "*.exe" -delete find $DESTINATION -type f -iname "*.html" -delete find $DESTINATION -type f -iname "*.htm" -delete find $DESTINATION -type f -iname "*.sfv" -delete find $DESTINATION -type f -iname "*.parts" -delete find $DESTINATION -type f -iname "*.jpg" -delete find $DESTINATION -type f -iname "*.png" -delete find $DESTINATION -type f -iname "*.doc" -delete sleep 10 # Step 3 - Change permissions of new files in Destination folder echo Changing ownership and permissions to Destination folder and files chown -R $user:$group "$DESTINATION" chmod -R $perms "$DESTINATION" done Any guidance would be appreciated.
Thanks
EDIT...
I cannot for the life of me get this script to even see a new folder added to the watch folder. It literally does NOTHING. If I replace the close_write with create & moved_to and make no other changes it sees the folder and contents and processes correctly. I find this very odd.
In fact I even tried to make a small test script to see if I can get it to work with a if/elif/else statement but once again when I copy a folder in the watch folder the script does nothing (doesn't even make to the loop). If I put a file in then it provides the correct output.
Here is the test script I ran. Can someone else confirm if they can get a new folder to be recognized and processed correctly?
#!/bin/bash WATCHED=/mnt/Watched inotifywait -re close_write --format '%w%f' -m $STEP1_WATCHED \ | while read -r new do if [[ -d "$new" ]]; then echo "Detected new folder $new" elif [[ -f "$new" ]]; then echo "Detected new file $new" else echo "neither $new" fi done done
close_writeandmoved_to?createas well asmoved_towithclose_writebut thenclose_writedoes not work because the others are true. Meaning it will just start running the script without waiting for the files to finish copying first. Thanks for the help!