成功复制后如何删除 bash 中的文件夹 (Mac OSX)?

发布于 2024-08-26 03:44:08 字数 1004 浏览 1 评论 0原文

我最近创建了我的第一个 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2024-09-02 03:44:08

可能有很多方法可以做到这一点,但我会选择:

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

&& 语法意味着只有在上一个命令成功退出时才执行此操作,并且 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文