【问题标题】:How to get file creation date in Linux?如何在 Linux 中获取文件创建日期?
【发布时间】:2011-05-08 18:33:17
【问题描述】:

我正在处理成批的文件,这些文件包含同一对象在其生命的不同时期的信息,唯一的排序方法是按创建日期。 我正在使用这个:

//char* buffer has the name of file
struct stat buf;
FILE *tf;
tf = fopen(buffer,"r");
//check handle
fstat(tf, &buf);
fclose(tf);
pMyObj->lastchanged=buf.st_mtime;

但这似乎不起作用。 我究竟做错了什么?还有其他更可靠/更简单的方法来获取 Linux 下的文件创建日期吗?

【问题讨论】:

  • fstat 不获取“文件创建”时间戳值,因为许多文件系统不跟踪该数据。您正在使用什么文件系统?
  • 我想这是最新 Ubuntu 桌面的标准配置 - 我在虚拟机(确切地说是 vmware 播放器)上运行我的代码,并将文件系统等所有细节留给 ubuntu 安装程序。跨度>
  • 试试stat(buffer, &buf),而不是这里没用的fopen
  • 如果该代码在没有警告的情况下编译,则说明您的编译器存在重大问题。如果该代码在编译时出现警告,请学习注意有关不正确参数类型(或指向整数转换的指针)的警告并修复代码,使其编译时不会出现警告。

标签: c linux


【解决方案1】:

最接近“创建日期”的近似值是struct stat 中的st_ctime 成员,但它实际上记录了inode 上次更改的时间。如果您创建文件并且从不修改其大小或权限,则作为创建时间。否则,没有文件创建时间的记录,至少在标准 Unix 系统中是这样。

出于您的目的,按st_mtime...排序或获取名称中带有时间戳的文件。


请注意,如果您使用的是 Darwin (Mac OS X),则可以使用创建时间。来自stat(2) 的手册页:

但是,当定义宏_DARWIN_FEATURE_64_BIT_INODE 时,stat 结构现在将定义为:

 struct stat { /* when _DARWIN_FEATURE_64_BIT_INODE is defined */
     dev_t           st_dev;           /* ID of device containing file */
     mode_t          st_mode;          /* Mode of file (see below) */
     nlink_t         st_nlink;         /* Number of hard links */
     ino_t           st_ino;           /* File serial number */
     uid_t           st_uid;           /* User ID of the file */
     gid_t           st_gid;           /* Group ID of the file */
     dev_t           st_rdev;          /* Device ID */
     struct timespec st_atimespec;     /* time of last access */
     struct timespec st_mtimespec;     /* time of last data modification */
     struct timespec st_ctimespec;     /* time of last status change */
     struct timespec st_birthtimespec; /* time of file creation(birth) */
     off_t           st_size;          /* file size, in bytes */
     blkcnt_t        st_blocks;        /* blocks allocated for file */
     blksize_t       st_blksize;       /* optimal blocksize for I/O */
     uint32_t        st_flags;         /* user defined flags for file */
     uint32_t        st_gen;           /* file generation number */
     int32_t         st_lspare;        /* RESERVED: DO NOT USE! */
     int64_t         st_qspare[2];     /* RESERVED: DO NOT USE! */
 };

注意st_birthtimespec 字段。还要注意,所有时间都在struct timespec 值中,因此存在亚秒级计时(tv_nsec 提供纳秒分辨率)。 POSIX 2008 <sys/stat.h> 要求 struct timespec 时间保持在标准时间;达尔文紧随其后。

【讨论】:

  • 遗憾的是,这些文件是由另一个应用程序创建的。可悲的是,我无权更改它的任何内容。
【解决方案2】:

fstat 适用于文件描述符,而不是 FILE 结构。最简单的版本:

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

#ifdef HAVE_ST_BIRTHTIME
#define birthtime(x) x.st_birthtime
#else
#define birthtime(x) x.st_ctime
#endif

int main(int argc, char *argv[])
{
        struct stat st;
        size_t i;

        for( i=1; i<argc; i++ )
        {
                if( stat(argv[i], &st) != 0 )
                        perror(argv[i]);
                printf("%i\n", birthtime(st));
        }

        return 0;
}

您需要通过检查 sys/stat.h 或使用某种 autoconf 构造来确定您的系统的统计结构中是否包含 st_birthtime。

【讨论】:

  • 那么我如何获取文件描述符呢?
  • @srv19 您可以使用 open(2) 或使用 fileno(tf) 获取文件描述符。但是,我给您的是一种无需打开文件即可检查其属性的方法。如果您仍然需要打开文件并想使用 stdio 功能,那么 fileno 是您的朋友。
  • 您的解决方案解决了我的问题。非常感谢。我怀疑我是在使用 fstat 执行任务。
【解决方案3】:

文件创建时间不存储在任何地方,您只能检索以下之一:

time_t    st_atime;   /* time of last access */
time_t    st_mtime;   /* time of last modification */
time_t    st_ctime;   /* time of last status change */

但是,您的代码应该为您提供最后修改时间。注意:您可以使用stat() 代替fstat() 而无需打开文件(stat() 将文件名作为参数)。

【讨论】:

  • 呸,我忽略了您将 FILE 指针传递给 fstat,而不是描述符 :)
【解决方案4】:

在linux中获取文件创建日期,我使用下面的方法

root@sathishkumar# cat << _eof > test.txt 
> Hello
> This is my test file
> _eof
root@sathishkumar# cat test.txt 
Hello
This is my test file
root@sathishkumar# ls -i test.txt 
2097517 test.txt
root@sathishkumar# debugfs -R 'stat <2097517>' /dev/sda5

Inode: 2097517   Type: regular    Mode:  0664   Flags: 0x80000
Generation: 4245143992    Version: 0x00000000:00000001
User:  1000   Group:  1000   Size: 27
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 8
Fragment:  Address: 0    Number: 0    Size: 0
 ctime: 0x50ea6d84:4826cc94 -- Mon Jan  7 12:09:00 2013
 atime: 0x50ea6d8e:75ed8a04 -- Mon Jan  7 12:09:10 2013
 mtime: 0x50ea6d84:4826cc94 -- Mon Jan  7 12:09:00 2013
 crtime: 0x5056d493:bbabf49c -- Mon Sep 17 13:13:15 2012
Size of extra inode fields: 28
EXTENTS:
(0):8421789

atime: 上次打开或执行文件的时间

ctime:inode 信息更新的时间。当文件被修改时,ctime 也会更新

mtime: 最后修改时间

crtime:文件创建时间

【讨论】:

  • 感谢您的回答,但我必须注意,我的问题是关于 linux core c 函数的使用,而不是如何在 shell 中获取创建时间。
猜你喜欢
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2011-06-13
  • 2013-07-07
  • 2014-01-28
  • 2013-01-28
  • 1970-01-01
相关资源
最近更新 更多