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

Srv*_*v19 9 c 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;
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*ler 10

最接近'创建日期'的是该st_ctime成员struct stat,但实际上记录了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! */
 };
Run Code Online (Sandbox Code Playgroud)

注意该st_birthtimespec字段.另请注意,所有时间都是struct timespec值,因此存在亚tv_nsec秒级时间(给出纳秒分辨率).POSIX 2008 <sys/stat.h>要求struct timespec时间保持标准时间; 达尔文就是这样.


Mel*_*Mel 8

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;
}
Run Code Online (Sandbox Code Playgroud)

您需要通过检查sys/stat.h或使用某种autoconf构造来确定您的系统是否在其stat结构中具有st_birthtime.


Sat*_*ish 6

要在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
Run Code Online (Sandbox Code Playgroud)

atime:上次打开或执行文件

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

mtime:上次修改时间

crtime:文件创建时间

  • 谢谢你的回答,但我必须注意,我的问题是关于linux核心c函数的使用,而不是如何在shell中获取创建时间. (2认同)

Cod*_*ers 5

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

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 */
Run Code Online (Sandbox Code Playgroud)

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