gpt4 book ai didi

linux - 为什么 find 的 -exec 选项包含 'non-matched' 项?

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:14 24 4
gpt4 key购买 nike

我正在尝试使用 find 来查找并排除/过滤一些目录,以免其复制到另一个备份目录。我尝试使用 find 的“-exec”选项来执行此操作,最终会复制每个已处理的文件,而不仅仅是匹配的文件,因此我对预期的行为应该是什么感到非常困惑,并且希望能帮助您更好地理解。

起点:

me@computer>ls
AddMonitorsOnEntry MantisCoreFormatting MantisGraph PastePicture XmlImportExport

确保查找按预期排除不需要的"file"

me@computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport'
.
./AddMonitorsOnEntry
./PastePicture

现在将这 2 目录复制到备份目录:

me@computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport' -exec cp -dr '{}' ~/backup \;

现在看看它是否有效......

me@computer>cd ~/backup
me@computer>ls
AddMonitorsOnEntry backup MantisCoreFormatting MantisGraph PastePicture XmlImportExport

什么??根据手册页中的这段代码,我认为“-exec”仅对匹配项进行操作:“...指定的命令针对每个匹配的文件运行一次...”

我知道还有其他方法可以完成此任务,但“-exec”似乎对于此处的海报 https://unix.stackexchange.com/questions/50612/how-to-combine-2-name-conditions-in-find/50633 来说效果很好。 。我正在寻求帮助来理解如何使用“-exec”与使用 xargs 或其他内容。谢谢。

最佳答案

Now to copy those 2 directories to a backup dir

您没有 2 场比赛。您的命令显示 3:

  1. .
  2. ./AddMonitorsOnEntry
  3. ./PastePicture

. 是当前目录,因此您的 cp 命令会复制所有内容。

您可以使用 find * 来跳过当前目录 .,而不是 find .,但仍处理其中的所有(非隐藏)文件/目录。

关于linux - 为什么 find 的 -exec 选项包含 'non-matched' 项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59957273/

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