views:

228

answers:

1

Hello! I recently created my first bash script, and I am having problems perfecting it's operation. I am trying to copy certain folders from one local drive, to a network drive. I am having the problem of deleting folders once they are copied over, sometimes the folder would be empty of contents, but the folder itself would remain, other times nothing would happen at all. Is there a better way to try to delete folders after rsync is done copying? I was trying to exclude the live tv buffer folder, but really, I can blow it away without consequence if need be. Any help would be great! thanks!

#!/bin/bash

network="CBS"
useracct="tvcapture"
thedate=$(date "+%m%d%Y")
folderToBeMoved="/users/$useracct/Documents"
newfoldername="/Volumes/Media/TV/$network/$thedate"

ECHO "Network is $network"
ECHO "date is $thedate"
ECHO "source is $folderToBeMoved"
ECHO "dest is $newfoldername"

mkdir $newfoldername
rsync -av $folderToBeMoved/"EyeTV Archive"/*.eyetv $newfoldername --exclude="Live TV Buffer.eyetv"

# this fails when there is more than one *.eyetv folder
if [ -d $newfoldername/*.eyetv ]; 
then 
#this deletes the contents of the directories
find $folderToBeMoved/"EyeTV Archive"/*.eyetv \( ! -path $folderToBeMoved/"EyeTV Archive"/"Live TV Buffer.eyetv" \) -delete
#remove empty directory
find $folderToBeMoved/"EyeTV Archive"/*.eyetv -type d -exec rmdir {} \;
fi
+2  A: 

There are probably lots of ways to do this, but I would go with:

rsync -av $folderToBeMoved/"EyeTV Archive"/*.eyetv $newfoldername --exclude="Live TV Buffer.eyetv" && rm -rf $folderToBeMoved

The && syntax means only do this is the previous command exited successfully, and rm -rf will force the removal of directories, even if there are files in them.

You could also try the rsync option --remove-source-files, but I believe that will leave you with the directories, so you'll still need to clean them up afterwards.

And as mentioned in the comment above, you want to avoid UPPERCASE commands like ECHO, cause it will cause things to break in a lot of places.

For details on exit status as well as lots of other great tips when working with bash, I always end up going back to http://tldp.org/LDP/abs/html/exit-status.html.

Good luck,

--jed

Jed Daniels