Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file creation date in Linux?

Tags:

c

linux

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?

like image 260
Srv19 Avatar asked May 08 '11 18:05

Srv19


People also ask

How can you find the creation date of a file Linux?

The easiest way to get the file creation date is with the stat command. As we can see, the creation date is shown in the “Birth” field.

How can you find the creation date of a file?

Windows file properties You can also see the modified date by viewing the file properties. Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.

How can I tell who created a file in Linux?

A. You can use ls -l command (list information about the FILEs) to find our the file / directory owner and group names. The -l option is known as long format which displays Unix / Linux / BSD file types, permissions, number of hard links, owner, group, size, date, and filename.


1 Answers

The nearest approximation to 'creation date' is the st_ctime member 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.

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


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

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

 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_birthtimespec field. Note, too, that all the times are in struct timespec values, so there is sub-second timing (tv_nsec gives nanosecond resolution). POSIX 2008 <sys/stat.h> requires the struct timespec time keeping on the standard times; Darwin follows that.

like image 185
Jonathan Leffler Avatar answered Sep 18 '22 08:09

Jonathan Leffler