Shell 如何指定;“如果目录不为空”;用灰壳脚本?

Shell 如何指定;“如果目录不为空”;用灰壳脚本?,shell,Shell,我正在写一个shell脚本。我的脚本进入一个目录,但我希望它只在目录包含任何数据(目录不是空的)的情况下继续并执行下一个命令。否则就不能再继续了。如何在shell脚本中指定这样的条件 if [ "$(ls -A $DIR)" ]; then echo "Take action $DIR is not Empty" else echo "$DIR is Empty" fi 这将检查目录和子目录查找目录-最大深度1-类型f将仅检查dir 如果要计算子目录和文件数: find di

我正在写一个shell脚本。我的脚本进入一个目录,但我希望它只在目录包含任何数据(目录不是空的)的情况下继续并执行下一个命令。否则就不能再继续了。如何在shell脚本中指定这样的条件

if [ "$(ls -A $DIR)" ]; then
     echo "Take action $DIR is not Empty"
else
    echo "$DIR is Empty"
fi
这将检查目录和子目录<代码>查找目录-最大深度1-类型f将仅检查
dir

如果要计算子目录和文件数:

find dir -mindepth 1 -maxdepth 1
将test与“$(ls-A$DIR)”一起使用,如:


使用grep:if find dir-typef | grep-q.比使用管道连接wc-l更干净。;然后。。。;fi+1注意,
-A
不是POSIX,而是GNU支持的BSD引入的扩展
find dir -mindepth 1 -maxdepth 1
if [ "$(ls -A $DIR)" ]; then
     echo "directory is not empty"
else
    echo "directory is Empty"
fi