# 文件时间信息
Date: 2020-07-30


文件时间信息一般包含，文件创建时间，文件修改时间，文件访问时间。 Unix/Linux 文件修改时间有两个一个是(Modify Time 文件内容修改/ Change Time:文件权限状态/权限), 根据不同的文件系统，文件时间信息属性会有一些差别。Unix/Linux在挂载文件系统的时候会设置相关文件时间属性相关参数。

## File Times in Linux

- 定义

Linux 早期内核版本也没有创建日期，在最新版本内核中已经可以看到相关字段

/include/linux/stat.h

```C
struct kstat {
    struct timespec64 atime;
    struct timespec64 mtime;
    struct timespec64 ctime;
    struct timespec64 btime;            /* File creation time */
};
```

不过glibc中没有相关接口

```C
struct stat
  {
    /* These are the members that POSIX.1 requires.  */
...
    __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.  */
...
  };
```

- 相关命令

**ls**

```C
access time   ls -lu 
modify time   ls -l
change time   ls -lc 
```

**stat**

```C
# stat a.txt
  File: ‘a.txt’
  Size: 133             Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 14998       Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-03-05 00:09:03.994629362 +0000
Modify: 2019-09-12 03:54:41.082515621 +0000
Change: 2019-09-12 03:54:56.284407466 +0000
 Birth: -
```

**debugfs** （仅ex4文件系统)

```C
$ sudo debugfs -R 'stat <14420015>' /dev/sda10

Inode: 14420015   Type: regular    Mode:  0777   Flags: 0x80000
Generation: 2130000141    Version: 0x00000000:00000001
User:  1000   Group:  1000   Size: 260
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 8
Fragment:  Address: 0    Number: 0    Size: 0
 ctime: 0x579ed684:8fd54a34 -- Mon Aug  1 10:26:36 2016
 atime: 0x58aea120:3ec8dc30 -- Thu Feb 23 14:15:20 2017
 mtime: 0x5628ae91:38568be0 -- Thu Oct 22 15:08:25 2015
crtime: 0x579ed684:8fd54a34 -- Mon Aug  1 10:26:36 2016
Size of extra inode fields: 32
EXTENTS:
(0):57750808
(END)
```

**find**

可以通过这三个时间查询需要文件

```C
-daystart
       Measure  times  (for  -amin,  -atime,  -cmin, -ctime,
        -mmin, and -mtime) from the beginning of today rather
        than from 24 hours ago.  This option only affects tests
       which appear later on the command line.
```

例子

```C
# ls -lu test6.txt
-rw-r--r--. 1 root root 5 Jul 30 16:53 test6.txt
# stat test6.txt
  File: test6.txt
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 102918054   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-07-30 16:53:23.010215539 +0800
Modify: 2019-08-29 14:08:49.483951624 +0800
Change: 2019-08-29 14:08:49.483951624 +0800
 Birth: -
# date
Thu Jul 30 17:03:51 CST 2020
# find . -name "*.txt" -amin 10
# find . -name "*.txt" -amin 11
./test6.txt
```

## File Times in AIX

- 定义:

AIX系统中没有创建时间`Create Time`概念

```C
/usr/include/sys/stat.h :

st_atime    Time when file data was last accessed.
st_mtime    Time when file data was last modified.
st_ctime    Time when the file metadata was last changed.
            such as permissions or ownership
```

- 相关命令

**ls**

```C
access time   ls -lu 
modify time   ls -l
change time   ls -lc 
```

**istat**

```C
$ istat p.out
Inode 263 on device 10/8        File
Protection: rw-r--r--
Owner: 0(root)          Group: 0(system)
Link count:   1         Length 14682 bytes

Last updated:   Tue Sep 15 10:50:15 PDT 2009
Last modified:  Tue Sep 15 10:50:15 PDT 2009
Last accessed:  Tue Nov  3 12:01:12 PST 2009
```

- 挂载

由于更新访问时间会有性能损耗， 所有可以通过以下两种方式设置不更新文件的`access time`

挂在文件系统时设置

```C
-o noatime
```

永久性修改

```C
chfs -a options=noatime
```

## File Times in Windows

`NTFS`文件系统使用的是`UTC`时间，不受时区和夏令时的影响， `FAT`文件系统,则及依赖计算机本地时间。`NTFS`会延时更新文件访问时间, 一般一个小时内不会更新访问时间。

可以通过`SetFileTime`来设置文件时间。

## 参考及引用

[File Times in AIX](https://www.ibm.com/support/pages/file-times-aix) [DEBUGFS Command – Show File Creation Times in Linux](https://www.tecmint.com/debugfs-command-show-file-creation-time-in-linux/) [Find Files By Access, Modification Date / Time Under Linux or UNIX](https://www.cyberciti.biz/faq/howto-finding-files-by-date/) [Windows - File Times](https://docs.microsoft.com/en-us/windows/win32/sysinfo/file-times)

