成功复制后如何删除 bash 中的文件夹 (Mac OSX)?
我最近创建了我的第一个 bash 脚本,但在完善它的操作时遇到了问题。我正在尝试将某些文件夹从一个本地驱动器复制到网络驱动器。
我遇到了复制文件夹后删除文件夹的问题,有时文件夹中的内容为空,但文件夹本身会保留,有时什么也不会发生。 rsync 完成复制后是否有更好的方法尝试删除文件夹?我试图排除直播电视缓冲区文件夹,但实际上,如果需要的话,我可以将其吹走而不会产生任何后果。
#!/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
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.
#!/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
可能有很多方法可以做到这一点,但我会选择:
&&
语法意味着只有在上一个命令成功退出时才执行此操作,并且rm -rf
> 将强制删除目录,即使其中有文件。您也可以尝试 rsync 选项 --remove-source-files,但我相信这会给您留下目录,因此您仍然需要在之后清理它们。
正如上面评论中提到的,您需要避免使用 ECHO 之类的大写命令,因为它会导致很多地方出现问题。
有关退出状态的详细信息以及使用 bash 时的许多其他重要提示,我总是最终返回 http://tldp.org/LDP/abs/html/exit-status.html。
There are probably lots of ways to do this, but I would go with:
The
&&
syntax means only do this is the previous command exited successfully, andrm -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.