I am monitoring a folder ($WATCHED) for new files (only .mkv)/folders. When something shows up it is to copy them to a Destination folder ($DESTINATION) and then change the ownership and permissions.
Here is the script I am using.
#!/bin/bash
WATCHED=/mnt/Watched
DESTINATION=/mnt/Destination
user=usrid
group=grpid
perms=755
inotifywait -re CLOSE_WRITE,CREATE,MOVED_TO --format $'%e\t%w%f' -m $WATCHED |
while IFS=$'\t' read -r events new
do
# Detecting and copying new files/folders
sleep 5
if [[ -d "$new" ]]
then
echo "Folder Detected: $new, Copying to Destination folder"
cp -r "$new" "$DESTINATION/"
elif [[ "$events" =~ CLOSE_WRITE ]]
then
if [ -f "$new" ] && [ "${new##*.}" == mkv ]
then
echo "File Detected: $new, Copying to Destination folder"
cp -r "$new" "$DESTINATION/"
elif [[ -e "$new" ]]
then
echo "New unknown item '$new'"
fi
fi
echo "Changing ownership and permissions for $DESTINATION"
chown -R $user:$group "$DESTINATION"
chmod -R $perms "$DESTINATION"
echo
done
I am having a couple of issues with this script.
- The changing ownership/permissions run three times for some reason when there is only one folder and one file in the folder. You can see from the output below that the second instance of this is by itself. Not sure why? I know that my placement and action for this is incorrect for what I would like to achieve. I would prefer that the ownership/permissions are done for each new individual file/folder when they are copied and not the entire destination folder. I am not sure the correct way of changing the ownership/permissions on each new file/folder only. Any guidance here would be very much appreciated.
- This script copies the folder and file to the directory using the correct/same directory tree BUT it also copies the file to the destination folder as well. So I have two copies of the file. First, why is it making a copy directly in the destination folder, and second, how do I stop it from doing that?
Here is the output.
Folder Detected: /mnt/Watched/NewFolder, Copying to the Destination folder
Changing ownership and permissions for /mnt/Destination
Changing ownership and permissions for /mnt/Destination
File Detected: /mnt/Watched/NewFolder/NewFile.mkv, Copying to Destination folder
Changing ownership and permissions for /mnt/Destination
This is how I would like the output to look
Folder Detected: /mnt/Watched/NewFolder, Copying to the Destination folder
Changing ownership and permissions for /mnt/Destination/NewFolder
File Detected: /mnt/Watched/NewFolder/NewFile.mkv, Copying to Destination folder
Changing ownership and permissions for /mnt/Destination/NewFolder/NewFile.mkv
Thanks
echo "New file '$item'"
line to perform yourmkdir
,cp
,chown
,chgrp
. Stop trying to copy a directory with its files when (only) the directory has been created - let the events for each file tell you when they're ready to be copied. In fact, don't even bother catching directory creation events - just create intermediate directories when you copy a file.