linux获取文件创建时间 api

在 Linux 中,获取文件创建时间的方法取决于文件系统类型。对于某些文件系统,可能没有创建时间的概念。大多数常见的文件系统,如 ext4 和 xfs,都记录了文件的创建时间(也称为“ctime”)。

要在 Linux 中获取文件创建时间,可以使用以下命令:

stat -c %w filename

其中,“%w”是格式化字符串,它将 ctime 格式化为人类可读的时间。执行此命令后,将返回文件的创建时间。

如果您需要在程序中使用该功能,可以使用 C 语言的 stat() 函数来获取文件的状态信息,其中包括文件的创建时间。下面是一个示例程序,演示如何使用 stat() 函数来获取文件的创建时间:

#include <stdio.h>
#include <sys/stat.h>
int main() {
    struct stat file_stat;
    char *filename = "file.txt";
    if (stat(filename, &file_stat) == 0) {
        printf("File creation time: %s", ctime(&file_stat.st_ctime));
    return 0;

在此程序中,我们使用 stat() 函数来获取文件的状态信息,并从中提取文件的创建时间(st_ctime)。然后,我们使用 ctime() 函数将时间转换为人类可读的格式,并将其输出到控制台。

  •