ls dirname
如果返回为空则目录为空
或者ls dirname|wc -l
如果结果为0则目录为空
最简单的方法:用ls -A $dir
好用的,测试过,对.file也能好用。
#!/bin/sh
#ifdirec
DIRECTORY=$1
#在此加上是不是目录的判断。
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
else
echo "$DIRECTORY is not empty"
fi
https://blog.csdn.net/jfkidear/article/details/42737053
分类: Linux2010-05-29 17:06 3512人阅读 评论(0) 收藏 举报
刚开始写shell,很多都不会在网上东找找西找找.
#判断文件目录是否为空
第一种:
emptydir.sh
-----------------------------------------------------------
#!/bin/sh
DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
else
echo "$DIRECTORY is not empty"
fi
-----------------------------------------------------------
第二种:
count.sh
-----------------------------------------------------------
#!/bin/sh
count=`ls $*|wc -w`
if [ "$count" > "0" ];
then
echo "file size $count"
else
echo "empty!"
fi
-----------------------------------------------------------
#目录是否存在
ifmkdir.sh
-----------------------------------------------------------
#!/bin/sh
dir="test"
if [ ! -d $dir ]; then
echo "$dir not exists"
mkdir "$dir"
else
echo "$dir exists!"
fi
-----------------------------------------------------------