免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: shitian8848
打印 上一主题 下一主题

如何统计一个目录下有多少文件??? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2004-10-18 14:36 |只看该作者

如何统计一个目录下有多少文件???

int List(char *pcPath)
{
        DIR *pstDir;
        int i,j;
        struct dirent *pLink;
        struct stat stStat;
        char   caFile[256];

        pstDir = opendir(pcPath);

        if (pstDir == NULL)
        {
                fprintf(stderr, "find dir [%s] failed!\n", pcPath);
                return( -1 );
        }

        i = 0;
        j = 0;
        while ((pLink = readdir(pstDir)) != NULL)
        {
                bzero(&stStat, sizeof(stStat));
                bzero(caFile, sizeof(caFile));
                sprintf(caFile, "%s/%s", pcPath, pLink->;d_name);
                stat(caFile, &stStat);
                if (S_ISDIR(stStat.st_mode) == 0)
                {
                        fprintf(stdout, "[No.%4d]-文件[%s]\n", ++i, pLink->;d_name);
                }
                else
                {
                        fprintf(stdout, "[No.%4d]-目录[%s]\n", ++j, pLink->;d_name);
                }
        }

        closedir(pstDir);
        return( 0 );
}

论坛徽章:
0
12 [报告]
发表于 2004-10-19 09:05 |只看该作者

如何统计一个目录下有多少文件???

楼上的太麻烦,看我的(statdir):

echo -n "total dirs(include self): "
find $1 -type d|wc -l

echo -n "total normal files:       "
find $1 -type f|wc -l

echo -n "total links:              "
find $1 -type l|wc -l

echo -n "total characters:         "
find $1 -type c|wc -l

echo -n "total blocks:             "
find $1 -type b|wc -l

echo -n "total name pipes:         "
find $1 -type p|wc -l

echo -n "total sockets:            "
find $1 -type s|wc -l

使用方法:
#statdir /dev 回车
total dirs(include self): 47
total normal files:       2
total links:              33
total characters:         1999
total blocks:             7831
total name pipes:         1
total sockets:            2

OK!

论坛徽章:
0
13 [报告]
发表于 2004-10-19 09:30 |只看该作者

如何统计一个目录下有多少文件???

好啊!

论坛徽章:
0
14 [报告]
发表于 2004-10-21 12:24 |只看该作者

如何统计一个目录下有多少文件???

I write a shell script as below:
#!/bin/sh
num=0
for file in `ls`
do
    if [ -f $file ] ; then
        echo `ls -l $file`
        num=`expr $num + 1`
    fi
done
echo "Total $num files in your directory."

:!: Please run it on the directory that you expect to calculate number of files.


[quote="shitian8848"]如题~
谢谢~[/quote]

论坛徽章:
0
15 [报告]
发表于 2004-11-01 18:08 |只看该作者

如何统计一个目录下有多少文件???

原帖由 "bst" 发表:
总结一下:
ls -l|wc -l 统计当前目录下的非隐藏目录和非隐藏文件的个数要在统计结果后减去一.

ls -la |wc -l
统计当前目录下的所有文件和目录的个数实际数目也是总数减去一

find ./ -type f -maxdepth 1 |wc..........



呵呵

[ 本帖最后由 exp07 于 2009-2-27 12:35 编辑 ]

论坛徽章:
3
CU大牛徽章
日期:2013-03-13 15:15:08CU大牛徽章
日期:2013-03-13 15:26:06CU大牛徽章
日期:2013-03-13 15:26:47
16 [报告]
发表于 2004-11-03 10:55 |只看该作者

如何统计一个目录下有多少文件???

太麻烦了吧,你们也!以下命令!
ls -F /grep -v /  |wc -l

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-11-10 06:20:00IT运维版块每日发帖之星
日期:2016-07-19 06:20:00JAVA
日期:2016-10-25 16:03:37PHP
日期:2016-10-25 16:03:37Python
日期:2016-10-25 16:03:37
17 [报告]
发表于 2010-08-25 22:23 |只看该作者
ls -l | awk '/^-/' | wc -l

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-11-10 06:20:00IT运维版块每日发帖之星
日期:2016-07-19 06:20:00JAVA
日期:2016-10-25 16:03:37PHP
日期:2016-10-25 16:03:37Python
日期:2016-10-25 16:03:37
18 [报告]
发表于 2010-08-25 22:24 |只看该作者

论坛徽章:
0
19 [报告]
发表于 2010-08-26 08:27 |只看该作者
ls -l | grep "^-" | wc -l

论坛徽章:
0
20 [报告]
发表于 2010-08-28 14:17 |只看该作者
我倒是觉得  find ./ -maxdepth 1 -type f |wc -l 更好点   
大家不妨试试两个比较一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP