gpt4 book ai didi

linux - 在 bash 脚本中查找命令导致目录出现 "No such file or directory"错误?

转载 作者:IT王子 更新时间:2023-10-29 00:18:10 28 4
gpt4 key购买 nike

2014 年 3 月 21 日更新

所以我意识到我没有尽可能高效,因为我需要“清理”的所有磁盘都在 /media 下并命名为“disk1 , disk2, disk3 等”这是最终脚本:

DIRTY_DIR="/media/disk*" 
find $DIRTY_DIR -depth -type d -name .AppleDouble -exec rm -rf {} \;
find $DIRTY_DIR -depth -type d -name .AppleDB -exec rm -rf {} \;
find $DIRTY_DIR -depth -type d -name .AppleDesktop -exec rm -rf {} \;
find $DIRTY_DIR -type f -name ".*DS_Store" -exec rm -f {} \;
find $DIRTY_DIR -type f -name ".Thumbs.db" -exec rm -f {} \; # I know, I know, this is a Windows file.

接下来可能会进一步清理代码,并添加日志记录和报告结果(通过电子邮件或其他方式)等功能;不包括系统和目录;并允许人们自定义文件/目录列表。

感谢大家的帮助!

更新

在采纳大家提供的有用建议之前,我做了一些测试,结果很有意思(见下文)。

作为测试,我运行了这个命令:

root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec echo rm -rf {} \;

结果(这是我所期望的):

rm -rf /media/disk3/Videos/Chorus/.AppleDouble

但是,当我运行实际命令时(没有echo):

root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;

我收到了相同的“错误”输出:

find: `/media/disk3/Videos/Chorus/.AppleDouble': No such file or directory

我将“error”放在引号中,因为很明显该文件夹已被删除,通过立即运行验证:

root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;
root@doi:~#

似乎 find 命令存储了原始结果,通过删除目录对其进行了操作,但随后又尝试将其删除?还是 rm-f 选项被忽略了,它应该是为了忽略不存在的文件和参数?我注意到,当我单独使用 rm 命令而不使用 find 命令运行测试时,一切都按预期工作。因此,直接运行rm -rf ...\nonexistent_directory,即使不存在“non_existent_directory”也没有返回错误,直接运行rm -r\nonexistent_directory提供预期的:

rm: cannot remove 'non_existent_directory': No such file or directory

我应该使用 -delete 选项而不是 -exec rm ... 选项吗?我想让脚本尽可能广泛地适用于没有 find-delete 选项的系统。

最后,我认为如果 /media/disk1, /media/disk2, ... 被组合在 下的 AUFS 文件系统中并不重要/media/storage 因为 find 命令是在各个磁盘上运行的吗?感谢到目前为止所有的帮助,伙计们。完成后我会发布脚本。

原帖

我正在编写一个 bash 脚本来删除我的 Lubuntu 文件共享上的一些 OS X 残余。但是,执行此操作时:

...
BASE_DIR="/media/disk" # I have 4 disks: disk1, disk2, ...
COUNTER=1

while [ $COUNTER -lt 5 ]; do # Iterate through disk1, disk2, ...
DIRTY_DIR=${BASE_DIR}$COUNTER # Look under the current disk counter /media/disk1, /media/disk2, ...
find $DIRTY_DIR -name \.AppleDouble -exec rm -rf {} \; # Delete all .AppleDouble directories
find $DIRTY_DIR -name ".*DS_Store" -exec rm -rf {} \; # Delete all .DS_Store and ._.DS_Store files
COUNTER=$(($COUNTER+1))
done
...

我看到以下输出:

找到:/media/disk1/Pictures/.AppleDouble:没有那个文件或目录

在我添加 -exec rm ... 部分之前,脚本找到了 /media/disk1/Pictures/.AppleDouble 目录。该脚本可以正常删除 DS_Store 文件,但是对于目录的 find 命令我缺少什么?

我不想在 -exec 部分搞砸太多,因为我不想错误地删除目录。

最佳答案

tl;dr - 如果您使用 find 删除目录,请传递 -prune

对于遇到这个问题的任何其他人。运行这样的示例

find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;

导致类似的错误

rm: cannot remove 'non_existent_directory': No such file or directory

用find查找和删除目录时,经常会遇到这个错误,因为find存储目录处理子目录,然后用exec删除,然后尝试遍历不再存在的子目录。

您可以传递 -maxdepth 0-prune 来防止此问题。像这样:

find /media/disk3 -type d -name .AppleDouble -prune -exec rm -rf {} \;

现在它删除目录没有任何错误。欢呼! :)

关于linux - 在 bash 脚本中查找命令导致目录出现 "No such file or directory"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462124/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com