bash - 如何检查目录是否有子目录?

标签 bash unix scripting

如何使用 bash 编写 if 语句来检查存储在名为“$DIR”的脚本变量中的某个目录是否包含不是“.”的子目录。还是“..”?

谢谢,-戴夫

最佳答案

正如评论所指出的,在过去的 9 年里情况发生了变化!点目录不再作为查找的一部分返回,而是 find 命令中指定的目录。

因此,如果您想继续使用这种方法:

#!/bin/bash
subdircount=$(find /tmp/test -maxdepth 1 -type d | wc -l)

if [[ "$subdircount" -eq 1 ]]
then
    echo "none of interest"
else
    echo "something is in there"
fi

(2011 年最初接受的答案)

#!/usr/bin/bash
subdircount=`find /d/temp/ -maxdepth 1 -type d | wc -l`

if [ $subdircount -eq 2 ]
then
    echo "none of interest"
else
    echo "something is in there"
fi

关于bash - 如何检查目录是否有子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6781225/

相关文章:

Linux if-else 不起作用

bash - 将二进制文件读入bash中的变量中

linux - 创建自定义接受行以测量命令在 zsh 中运行所花费的时间

sockets - "sin"在 sin_addr、sin_family 等中是什么意思?

linux - awk 将字符串添加到除最后一个空行之外的每一行

linux - 发送 ssh 命令时防止本地 shell 替换变量

macos - 如何在 OSX 上创建没有备份文件的内存映射文件?

c++ - 如何使用 C++ 实现 native 脚本

powershell - 我可以从脚本或命令行将文件复制到网上邻居吗?

file - 使用 SQL-Developer 时将脚本输出到文件