如何在C中获取目录的大小?

时间:2023-01-24 16:30:13

Is there a POSIX function that will give me the size of a directory (including all sub-folders), roughly equivalent to "du -s somepath"?

是否有一个POSIX函数可以给我一个目录(包括所有子文件夹)的大小,大致相当于“du -s somepath”?

3 个解决方案

#1


26  

$ man nftw

NAME

ftw, nftw - file tree walk

ftw,nftw - 文件树走

DESCRIPTION

ftw() walks through the directory tree that is located under the directory dirpath, and calls fn() once for each entry in the tree. By default, directories are handled before the files and subdirectories they contain (pre-order traversal).

ftw()遍历目录dirpath下的目录树,并为树中的每个条目调用fn()一次。默认情况下,目录在它们包含的文件和子目录之前处理(预订遍历)。

CONFORMING TO

POSIX.1-2001, SVr4, SUSv1.

POSIX.1-2001,SVr4,SUSv1。

Simple example

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

static unsigned int total = 0;

int sum(const char *fpath, const struct stat *sb, int typeflag) {
    total += sb->st_size;
    return 0;
}

int main(int argc, char **argv) {
    if (!argv[1] || access(argv[1], R_OK)) {
        return 1;
    }
    if (ftw(argv[1], &sum, 1)) {
        perror("ftw");
        return 2;
    }
    printf("%s: %u\n", argv[1], total);
    return 0;
}

#2


2  

There is no ready-made function, so you will have to make your own. You may look at the source code of the GNU implemenation of du as an example (see http://www.gnu.org/prep/ftp.html for a list of places to download from). It is in the coreutils package.

没有现成的功能,所以你必须自己做。您可以查看du的GNU实现的源代码作为示例(有关要下载的位置列表,请参阅http://www.gnu.org/prep/ftp.html)。它在coreutils包中。

The crucial Posix calls are probably opendir, readdir, closedir, and stat.

关键的Posix调用可能是opendir,readdir,closedir和stat。

#3


0  

Result in bytes:

结果以字节为单位

du -sb | grep -oE '^\s*[0-9]+'

#1


26  

$ man nftw

NAME

ftw, nftw - file tree walk

ftw,nftw - 文件树走

DESCRIPTION

ftw() walks through the directory tree that is located under the directory dirpath, and calls fn() once for each entry in the tree. By default, directories are handled before the files and subdirectories they contain (pre-order traversal).

ftw()遍历目录dirpath下的目录树,并为树中的每个条目调用fn()一次。默认情况下,目录在它们包含的文件和子目录之前处理(预订遍历)。

CONFORMING TO

POSIX.1-2001, SVr4, SUSv1.

POSIX.1-2001,SVr4,SUSv1。

Simple example

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

static unsigned int total = 0;

int sum(const char *fpath, const struct stat *sb, int typeflag) {
    total += sb->st_size;
    return 0;
}

int main(int argc, char **argv) {
    if (!argv[1] || access(argv[1], R_OK)) {
        return 1;
    }
    if (ftw(argv[1], &sum, 1)) {
        perror("ftw");
        return 2;
    }
    printf("%s: %u\n", argv[1], total);
    return 0;
}

#2


2  

There is no ready-made function, so you will have to make your own. You may look at the source code of the GNU implemenation of du as an example (see http://www.gnu.org/prep/ftp.html for a list of places to download from). It is in the coreutils package.

没有现成的功能,所以你必须自己做。您可以查看du的GNU实现的源代码作为示例(有关要下载的位置列表,请参阅http://www.gnu.org/prep/ftp.html)。它在coreutils包中。

The crucial Posix calls are probably opendir, readdir, closedir, and stat.

关键的Posix调用可能是opendir,readdir,closedir和stat。

#3


0  

Result in bytes:

结果以字节为单位

du -sb | grep -oE '^\s*[0-9]+'