linux 中 如何判断文件夹是否为空

 

001、

[root@PC1 test]# ls
[root@PC1 test]# mkdir test1 test2                 ## 测试文件夹
[root@PC1 test]# echo "xxx" > test2/a.txt
[root@PC1 test]# ls
test1  test2
[root@PC1 test]# tree
.
├── test1
└── test2
    └── a.txt

2 directories, 1 file
[root@PC1 test]# [ $(ls test1 | wc -l) -eq 0 ]        ## 如果文件夹为空,返回真
[root@PC1 test]# echo $?
0
[root@PC1 test]# [ $(ls test2 | wc -l) -eq 0 ]        ## 如果文件夹不为空,返回假
[root@PC1 test]# echo $?
1

 

posted @ 2022-12-08 23:17  小鲨鱼2018  阅读(854)  评论(0编辑  收藏  举报