如何在Linux中获取文件创建日期?

问题描述:

我正在处理批量的文件,这些文件包含有关同一对象在其生命不同时间的信息,唯一的排序方式是创建日期。 我正在使用这个:如何在Linux中获取文件创建日期?

//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下的文件创建日期吗?

+3

'fstat'不会因为许多文件系统不要取“文件创建”时间戳值”跟踪这些数据。你在使用什么文件系统? – 2011-05-08 18:36:57

+0

一个是最新的Ubuntu桌面的标准,我想 - 我正在虚拟机上运行我的代码(准确地说,是vmware播放器),并将所有的细节保存到Ubuntu安装程序中。 – Srv19 2011-05-08 18:41:41

+0

尝试'stat(buffer,&buf)'而不是这里没有用的'fopen' – mpez0 2011-05-08 19:51:48

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构建体在其stat结构st_birthtime。

+0

那么我如何获取文件描述符? – Srv19 2011-05-08 18:58:53

+1

@ srv19您可以使用open(2)或fileno(tf)来获取文件描述符。然而,我给你的是一种检查它的属性而不必打开文件的方法。如果您仍然需要打开该文件并想要使用stdio功能,那么fileno就是您的朋友。 – Mel 2011-05-08 19:05:00

+0

您的解决方案摆脱了我的问题。非常感谢。我怀疑我是使用fstat的使命。 – Srv19 2011-05-08 19:57:29

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

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()将文件名称作为参数)。

+0

Bah,我忽略了你将FILE指针传递给fstat,而不是描述符:) – 2011-05-08 19:24:08

最近的近似“创建日期”是在struct statst_ctime成员,但实际上记录最后一次inode的改变。如果您创建该文件并且从不修改其大小或权限,那么该文件将用作创建时间。否则,至少在标准的Unix系统中,没有创建文件的时间记录。

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


请注意,如果您是在达尔文(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时间保持在标准时间;达尔文遵循这一点。

+0

不幸的是,这些文件是由另一个应用程序创建的。可悲的是,我没有权限改变任何事情。 – Srv19 2011-05-08 18:48:27

要获得在Linux中的文件创建日期,我用下面的方法

[email protected]# cat <<_eof> test.txt 
> Hello 
> This is my test file 
> _eof 
[email protected]# cat test.txt 
Hello 
This is my test file 
[email protected]# ls -i test.txt 
2097517 test.txt 
[email protected]# 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:文件创建时间

+2

谢谢你的回答,但我必须注意到我的问题是关于使用linux核心c函数,而不是如何在shell中获得创建时间。 – Srv19 2013-01-08 14:08:17