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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5929419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:00:00  来源:igfitidea点击:

How to get file creation date in Linux?

clinux

提问by Srv19

I am working with batches of files that contain information about the same object at the different times of its life, and the only way to order them is by creation date. I was using this:

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

//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;

But that does not seems to work. What am I doing wrong? Are there other, more reliable/simple ways to get file creation date under Linux?

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

采纳答案by Mel

fstat works on file descriptors, not FILE structures. The simplest version:

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;
}

You will need to figure out if your system has st_birthtime in its stat structure by inspecting sys/stat.h or using some kind of autoconf construct.

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

回答by Code Painters

File creation time is not stored anywhere, you can only retrieve one of the following:

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

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 */

Your code should give you the last modification time, however. Note: you can use stat()instead of fstat()without opening the file (stat()takes the file name as param).

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

回答by Jonathan Leffler

The nearest approximation to 'creation date' is the st_ctimemember in the struct stat, but that actually records the last time the inode changed. If you create the file and never modify its size or permissions, that works as a creation time. Otherwise, there is no record of when the file was created, at least in standard Unix systems.

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

For your purposes, sort by st_mtime...or get the files named with a timestamp in the name.

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



Note that if you are on Darwin (Mac OS X), the creation time is available. From the man page for stat(2):

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

However, when the macro _DARWIN_FEATURE_64_BIT_INODEis defined, the stat structure will now be defined as:

但是,当_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! */
 };

Note the st_birthtimespecfield. Note, too, that all the times are in struct timespecvalues, so there is sub-second timing (tv_nsecgives nanosecond resolution). POSIX 2008 <sys/stat.h>requires the struct timespectime keeping on the standard times; Darwin follows that.

注意st_birthtimespec字段。还要注意,所有时间都是struct timespec值,所以有亚tv_nsec秒级计时(给出纳秒分辨率)。POSIX 2008<sys/stat.h>要求struct timespec时间保持在标准时间上;达尔文遵循这一点。

回答by Sathish

To get the file creation date in linux, I use the following method

在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: Last time file was opened or executed

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

ctime: Time the inode information was updated. ctime also gets updated when file is modified

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

mtime: Last modified time

mtime:最后修改时间

crtime: File creation time

crtime:文件创建时间