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

查看:536
本文介绍了在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 ... \nonexistent_directory,即使不存在"non_existent_directory",也不会返回任何错误,并且直接运行rm -r \nonexistent_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 ... \nonexistent_directory, no errors were returned even though the "non_existent_directory" was not there, and directly running rm -r \nonexistent_directory provided the expected:

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

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

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,...组合在/media/storage下的AUFS文件系统中很重要,因为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天全站免登陆