相关疑难解决方法(0)

为什么循环查找的输出是不好的做法?

这个问题的灵感来自

为什么使用 shell 循环处理文本被认为是不好的做法?

我看到这些结构

for file in `find . -type f -name ...`; do smth with ${file}; done
Run Code Online (Sandbox Code Playgroud)

for dir in $(find . -type d -name ...); do smth with ${dir}; done
Run Code Online (Sandbox Code Playgroud)

几乎每天都在这里使用,即使有些人花时间对这些帖子发表评论,解释为什么应该避免这种东西......
看到此类帖子的数量(以及有时这些评论被简单地忽略的事实)我想我还不如问一个问题:

为什么循环find的输出是不好的做法,为 返回的每个文件名/路径运行一个或多个命令的正确方法是find什么?

find filenames files for

193
推荐指数
4
解决办法
3万
查看次数

第一次匹配后如何停止查找命令?

有没有办法强制find命令在找到第一个匹配项后立即停止?

find command

166
推荐指数
3
解决办法
10万
查看次数

打印当前目录中不包含.git的所有子目录

我正在尝试编写一个 bash 行,它将查看当前文件夹中的所有子目录,并告诉我是否有不包含“.git”的子目录。伪:

for subdir in currentdir
  if .git does not exist
    print subdir
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试的单行代码,它不起作用并且似乎打印了所有子目录:

find . -maxdepth 1 -type d -execdir $SHELL -c '[ ! -d ".git" ] && echo "not git repo:" {}' $SHELL -c '{}' ';'
Run Code Online (Sandbox Code Playgroud)

我找到了一些解决方案,可以让我打印所有具有 .git 的子目录,就像这个- 我正在尝试做相反的事情。

我究竟做错了什么?这可能吗?

bash find git test exec

1
推荐指数
1
解决办法
661
查看次数

标签 统计

find ×3

bash ×1

command ×1

exec ×1

filenames ×1

files ×1

for ×1

git ×1

test ×1