Bash 检查目录是否仅包含目录并抑制输出

Bash 检查目录是否仅包含目录并抑制输出,bash,Bash,到目前为止,我得到的是: if [[ $(find $directory -type f) -eq 0 ]]; then echo "Specified directory contains only subdirectories and no file"; exit 1; fi 检查有效,但如果所述目录中确实有文件,我会看到命令“find”的输出。我不需要看到它,我只想知道所讨论的目录是否只包含目录,如果包含,则显示该消息。 我把这个检查放在目录上的for循环之前

到目前为止,我得到的是:

if [[ $(find $directory -type f) -eq 0 ]]; then
  echo "Specified directory contains only subdirectories and no file";
  exit 1;
fi
检查有效,但如果所述目录中确实有文件,我会看到命令“find”的输出。我不需要看到它,我只想知道所讨论的目录是否只包含目录,如果包含,则显示该消息。 我把这个检查放在目录上的for循环之前,这样如果没有文件,它就会终止


我觉得必须有一个简单的方法来实现这个结果,但是找不到任何具体的方法。您有解决方案吗?

您不需要
位于shell中行的末尾。而且,
find
也可以 不返回数字-您必须计算匹配数。这应该 工作:


非常感谢。难以置信的快速回答:)我知道,这是我的习惯。我就要这个;关闭。无需分叉另一个子壳来输送
wc-c
。只需执行
[[-z”$(查找“$directory”-键入f)”]
。这甚至适用于BSD Find,它不提供
-printf
@LéaGris:哦,在这种情况下也会这样。好主意。
if [[ "$(find "$directory" -type f -printf . | wc -c)"  -eq 0 ]]; then
  echo "Specified directory contains only subdirectories and no file"
  exit 1
fi