在 bash 脚本中查找命令导致“没有这样的文件或目录"仅针对目录的错误? [英] find command in bash script resulting in "No such file or directory" error only for directories?

查看:17
本文介绍了在 bash 脚本中查找命令导致“没有这样的文件或目录"仅针对目录的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2014-03-21 更新

所以我意识到我的效率不够高,因为我需要清理"的所有磁盘都在 /media 下并命名为disk1disk2disk3 等"这是最终的脚本:

So I realized I wasn't as efficient as I could be, as all the disks that I needed to "scrub" were under /media and named "disk1, disk2,disk3, etc." Here's the final script:

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.

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

Next will probably to just clean up the code even more, and add features like logging and reporting results (through e-mail or otherwise); excluding system and directories; and allowing people to customize the list of files/directories.

感谢大家的帮助!

更新

在采纳大家提供的有用建议之前,我进行了一些测试,结果非常有趣(见下文).

Before I incorporated the helpful suggestions provided by everyone, I performed some tests, the results of which were very interesting (see below).

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

As a test, I ran this command:

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

结果(这是我的预期):

The results (which is what I expected):

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

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

However, when I ran the actual command (without echo):

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

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

I received the same "error" output:

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

我将错误"放在引号中,因为显然该文件夹已被删除,并通过立即运行验证:

I put "error" in quotes because obviously the folder was removed, as verified by immediately running:

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

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

It seems like the find command stored the original results, acted on it by deleting the directory, but then tried to delete it again? Or is the -f option of rm, which is supposed to be for ignoring nonexistent files and arguments, is ignored? I note that when I run tests with the rm command alone without the find command, everything worked as expected. Thus, directly running rm -rf ... onexistent_directory, no errors were returned even though the "non_existent_directory" was not there, and directly running rm -r onexistent_directory provided the expected:

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

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

Should I use the -delete option instead of the -exec rm ... option? I had wanted to make the script as broadly applicable as possible for systems that didn't have -delete option for find.

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

Lastly, I don't presume it matters if /media/disk1, /media/disk2, ... are combined in an AUFS filesystem under /media/storage as the find command is operating on the individual disks themselves? Thanks for all the help so far, guys. I'll publish the script when I'm done.

原帖

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

I'm writing a bash script to delete a few OS X remnants on my Lubuntu file shares. However, when executing this:

...
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:没有这样的文件或目录

find: /media/disk1/Pictures/.AppleDouble: No such file or directory

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

Before I added the -exec rm ... portion the script found the /media/disk1/Pictures/.AppleDouble directory. The script works properly for removing DS_Store files, but what am I missing for the find command for directories?

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

I'm afraid to screw too much with the -exec portion as I don't want to obliterate directories in error.

推荐答案

您不需要在 shell glob 中转义 DOT,因为这不是正则表达式.所以使用 .AppleDouble 而不是 .AppleDouble:

You don't need to escape DOT in shell glob as this is not regex. So use .AppleDouble instead of .AppleDouble:

find $DIRTY_DIR -name .AppleDouble -exec rm -rf '{}' ;

PS:我看不到任何地方 $COUNTER 在您的脚本中递增.

PS: I don't see anywhere $COUNTER being incremented in your script.

这篇关于在 bash 脚本中查找命令导致“没有这样的文件或目录"仅针对目录的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆