linux用C如何鉴别一个目录能否为空
2011-03-07 18:33
106 查看
作者: opius 出自: http://www.linuxdiyf.com
用opendir掀开一个目录,失失机关DIR*,可是没有关于其下有多少文件、子目录的数据,我用比较土的措施,遍历目录readdir,角力计较争论其下有多少文件和子目录,当然也不是连子目录下的工具也找。
上面的挨次在solaris8、gcc编译经过议定的,若是一个目录是空的,输入为2。
#include
#include
#include
int main(int argc , char **argv)
{
DIR *dirp;
int num=0;
dirp = opendir(argv[1]);
while (dirp) {
if ( readdir(dirp) != NULL)
num;
else
break;
}
closedir(dirp);
printf("%d\n",num);
}
shell中鉴别目录为空
#!/bin/ksh
# Check if a directory is empty or not
if [ $# = 0 ]
then
echo
echo "use this tool to check if directory is empty"
echo "for example: isEmpty dirName "
echo
exit 1
fi
case $(( 0 $(find $1 2>&- |head -2|wc -l))) in
0) echo Permission denied ! ;;
1) echo Directory is empty ! ;;
*) echo Directory is not empty ! ;;
esac
版权声明:
原创作品,许诺转载,转载时请务必以超链接编制标明文章 原始出处 、作者信息和本声明。不然将追究法律责任。
用opendir掀开一个目录,失失机关DIR*,可是没有关于其下有多少文件、子目录的数据,我用比较土的措施,遍历目录readdir,角力计较争论其下有多少文件和子目录,当然也不是连子目录下的工具也找。
上面的挨次在solaris8、gcc编译经过议定的,若是一个目录是空的,输入为2。
#include
#include
#include
int main(int argc , char **argv)
{
DIR *dirp;
int num=0;
dirp = opendir(argv[1]);
while (dirp) {
if ( readdir(dirp) != NULL)
num;
else
break;
}
closedir(dirp);
printf("%d\n",num);
}
shell中鉴别目录为空
#!/bin/ksh
# Check if a directory is empty or not
if [ $# = 0 ]
then
echo
echo "use this tool to check if directory is empty"
echo "for example: isEmpty dirName "
echo
exit 1
fi
case $(( 0 $(find $1 2>&- |head -2|wc -l))) in
0) echo Permission denied ! ;;
1) echo Directory is empty ! ;;
*) echo Directory is not empty ! ;;
esac
版权声明:
原创作品,许诺转载,转载时请务必以超链接编制标明文章 原始出处 、作者信息和本声明。不然将追究法律责任。
相关文章推荐
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植2:如何新建一个项目,引擎目录结构介绍
- wordpress如何制作一个跟随滑动的文章目录?Content Index文章目录插件修改篇
- 如何用Java代码列出一个目录下所有的文件?
- 如何umount一个busy的目录
- linux下如何统计一个目录下的文件个数以及代码总行数的命令
- python小练习5:如何判断一个数能否被3整除
- [tips]xnix系统下如何将一个包含svn属性的目录还原成干净的目录
- VC++中如何获得一个当前进程的运行目录
- 如何取消一个本地svn目录与svn的联系(即恢复原有图标等)
- 如何证明一个数能否被7整除的判定方法
- MFC中 如何判断一个字符串是否是有效的目录 或盘符
- [Groovy]如何删除给一个目录
- 如何列出一个目录占用的空间
- 我如何添加一个空目录到Git仓库?
- 如何取消一个目录的git初始化
- 如何实现在任意一个目录下都可以执行想要执行的文件
- java中如何创建一个指定文件,一个指定的目录
- [随笔]Linux下,如何将一个文件及其所在的递归路径一起拷贝到目标目录?
- 多个域名指定到同一个IP下,访问时如何决定改跳到哪个文件目录
- 如何将Linux系统的多个目录及文件备份并压缩到一个文件,一方面保持和传递?