- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
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/
我用 IntelliJ IDEA 2021.1 CE 在 流行!_OS 20.04 与 bash 5.0.17 . 问题造句:我将IntelliJ终端设置为/bin/bash通过 IntelliJ 设
给定如下命令: bash --shortcuts 我想显示一个快捷方式列表,就像在这个页面上一样: http://www.skorks.com/2009/09/bash-shortcuts-for-m
我有一个脚本可以操作数据、创建参数并将它们发送到第二个脚本。其中一个参数包含一个空格。 脚本1.sh: args=() args+=("A") args+=("1 2") args+=("B") .
我的脚本的“只运行一次”版本的一个非常简单的示例: ./myscript.sh var1 "var2 with spaces" var3 #!/bin/bash echo $1 #output: va
我想了解数字( double )在 bash 中是如何表示的,以及当我在 bash 中以十六进制格式打印数字时会发生什么。 根据 IEEE 754 标准,double 应由 64 位表示:52 位(1
我试图在 bash -c "..." 命令中获取 bash 脚本,但它不起作用。 如果我在 bash -c "..." 之外运行命令,它会起作用。 我需要使用 bash -c "..." 因为我想确保
如何检测我的 bash shell 中是否加载了 bash 补全包?从 bash-completion 的 2.1 版(包含在 Debian 8 中)开始,除了 BASH_COMPLETION_COM
我的 bash_profile 中有一个投影函数。现在我试图从 bash 脚本中调用这个函数,但是我得到了一个未找到的错误。如何使投影函数对 bash 脚本可见? 最佳答案 必须导出函数 export
我正在编写一个 bash 脚本,它接受许多命令行参数(可能包括空格)并通过登录 shell 将它们全部传递给程序 (/bin/some_program)。从 bash 脚本调用的登录 shell 将取
当我创建一个新的 bash 进程时,提示符默认为一个非常简单的提示符。我知道我可以编辑 .bashrc 等来更改它,但是有没有办法使用 bash 命令传递提示? 谢谢! 最佳答案 提示由 PS1、PS
好的,我希望这个问题有一定道理,但是 bash shell 和 bash 终端之间有什么区别?例子。当我第一次打开终端时,会提示我当前的目录和用户名。在终端窗口标题中显示 -bash- ,当我键入 e
我是 SBCL 的新手,我正在尝试从 bash 终端运行存储在文本文件中的 Lisp 脚本。 这是我在文件开头写的内容 http://www.sbcl.org/manual/#Running-from
我知道我们可以在 bash 中使用将十六进制转换为十进制 #!/bin/bash echo "Type a hex number" read hexNum echo $(( 16#$hexNum ))
我正在尝试在 bash 脚本中自动完成文件夹名称。如果我输入完整的文件夹名称,一切正常,但我不知道如何自动完成名称。有什么想法吗? repo() { cd ~/Desktop/_REPOS/$1 }
我想检查远程网站上的一些文件。 这里是bash命令生成计算文件md5的命令 [root]# head -n 3 zrcpathAll | awk '{print $3}' | xargs -I {}
是否有任何内置函数可以使用 bash shell 脚本从给定日期获取下周日(下周一、下周二等)?例如,2014 年 9 月 1 日之后的第一个星期日是什么时候?我预计 2014 年 9 月 7 日。
我一直在尝试根据表格重命名一些特定文件,但没有成功。它要么重命名所有文件,要么给出错误。 该目录包含数百个以长条形码命名的文件,我只想重命名包含模式 _1_ 的文件。 例子 barcode_1_bar
bash 中有没有办法用变量的内容替换文本文件中的占位符? 例如,我想发送一封电子邮件通知,如下所示: Dear Foo, Alert: blah blah blah blah blah blah
我有一个 bash 脚本,它在某些字符串上附加了一个重音字符,导致它失败,我找不到这些字符在哪里或如何进入那里。 这是一些示例输出: mv: cannot move â/tmp/myapp.zipâ
这个问题在这里已经有了答案: How do I place stdout on edit line? (1 个回答) Can a bash script prepopulate the prompt
我是一名优秀的程序员,十分优秀!