- A+
[20201105]mtime, ctime and atime in Linux.txt
--//大家都知道linux 文件系统的文件存在3个时间mtime, ctime and atime。简单解析如下:
--//https://linoxide.com/file-system/file-timestamps-mtime-ctime-atime-linux/
mtime – Last modification time
Mtime or modification time is the time of the last change to the file contents. 'Modification' means something inside
the file was amended or deleted, or new data was added.
ctime – last change time
Ctime is the changed timestamp referring to changes made to attribute of a file such as ownership, access permission.
It's the time at which the metadata related to the file changed.
--//以前我总是理解c表示create,这是不对的。
atime – last access time
Atime or access timestamp is the last time a file was read, read by one of the processes directly or through commands
and scripts.
--//通过一个简单的例子说明:
1. 使用stat命令,这个是最简单的方法:
$ stat z6.txt
File: `z6.txt'
Size: 2652 Blocks: 8 IO Block: 4096 regular file
Device: 6802h/26626d Inode: 9209574 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 502/ oracle) Gid: ( 502/oinstall)
Access: 2020-04-29 08:51:47.000000000 +0800
Modify: 2019-12-30 09:41:44.000000000 +0800
Change: 2019-12-30 09:41:44.000000000 +0800
$ chmod 664 z6.txt
--//相当于修改ctime时间。
$ stat z6.txt
File: `z6.txt'
Size: 2652 Blocks: 8 IO Block: 4096 regular file
Device: 6802h/26626d Inode: 9209574 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 502/ oracle) Gid: ( 502/oinstall)
Access: 2020-04-29 08:51:47.000000000 +0800
Modify: 2019-12-30 09:41:44.000000000 +0800
Change: 2020-11-05 10:11:28.000000000 +0800
2.使用ls:
$ echo -l -lc -lu | tr ' ' 'n' |xargs -I Q ls --color=auto --time-style=+"%Y-%m-%d %H:%M:%S" Q z6.txt
-rw-rw-r-- 1 oracle oinstall 2652 2019-12-30 09:41:44 z6.txt --> mtime
-rw-rw-r-- 1 oracle oinstall 2652 2020-11-05 10:11:28 z6.txt --> ctime
-rw-rw-r-- 1 oracle oinstall 2652 2020-04-29 08:51:47 z6.txt --> atime
--//注意这个参数不能连用。也就是ls -l 缺省显示的mtime时间。
3.使用debugfs,不推荐这样的方式:
# mount | column -t
/dev/cciss/c0d0p2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/cciss/c0d0p6 on /u01 type ext3 (rw)
/dev/cciss/c0d0p1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
tmpfs on /mnt/ramdisk type tmpfs (rw,size=8G)
# debugfs -R 'stat /home/oracle/hrp430/testz/z6.txt' /dev/cciss/c0d0p2
debugfs 1.39 (29-May-2006)
Inode: 9209574 Type: regular Mode: 0664 Flags: 0x0 Generation: 1719813277
User: 502 Group: 502 Size: 2652
File ACL: 0 Directory ACL: 0
Links: 1 Blockcount: 8
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0x5fa35f50 -- Thu Nov 5 10:11:28 2020
atime: 0x5ea8cfa3 -- Wed Apr 29 08:51:47 2020
mtime: 0x5e0955d8 -- Mon Dec 30 09:41:44 2019
BLOCKS:
(0):9216200
TOTAL: 1
4.补充测试:
$ cat z6.txt
$ stat z6.txt
File: `z6.txt'
Size: 2652 Blocks: 8 IO Block: 4096 regular file
Device: 6802h/26626d Inode: 9209574 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 502/ oracle) Gid: ( 502/oinstall)
Access: 2020-11-05 10:19:23.000000000 +0800 -->改变了atime时间
Modify: 2019-12-30 09:41:44.000000000 +0800
Change: 2020-11-05 10:11:28.000000000 +0800
$ echo -l -lc -lu | tr ' ' 'n' |xargs -I Q ls --color=auto --time-style=+"%Y-%m-%d %H:%M:%S" Q z6.txt
-rw-rw-r-- 1 oracle oinstall 2652 2019-12-30 09:41:44 z6.txt
-rw-rw-r-- 1 oracle oinstall 2652 2020-11-05 10:11:28 z6.txt
-rw-rw-r-- 1 oracle oinstall 2652 2020-11-05 10:19:23 z6.txt -->atime.
--//实际上了解这些时间属性,可以知道什么时候做了一些改动,正常系统还是没有问题。
--//不过对于高手可以通过touch改动。
# man touch
...
DESCRIPTION
Update the access and modification times of each FILE to the current time.
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create
do not create any files
-d, --date=STRING
parse STRING and use it instead of current time
-f (ignored)
-m change only the modification time
-r, --reference=FILE
use this file's times instead of current time
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD
change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version
output version information and exit
Note that the -d and -t options accept different time-date formats.
If a FILE is -, touch standard output.
--// -a 改变atime,-m 改变mtime,ctime如何使用touch修改不知道。简单测试:
$ touch -a z6.txt
$ stat z6.txt
File: `z6.txt'
Size: 2652 Blocks: 8 IO Block: 4096 regular file
Device: 6802h/26626d Inode: 9209574 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 502/ oracle) Gid: ( 502/oinstall)
Access: 2020-11-05 10:26:49.000000000 +0800
Modify: 2019-12-30 09:41:44.000000000 +0800
Change: 2020-11-05 10:26:49.000000000 +0800
--//实际上ctime,atime都修改了。
$ touch -m z6.txt
$ stat z6.txt
File: `z6.txt'
Size: 2652 Blocks: 8 IO Block: 4096 regular file
Device: 6802h/26626d Inode: 9209574 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 502/ oracle) Gid: ( 502/oinstall)
Access: 2020-11-05 10:27:40.000000000 +0800
Modify: 2020-11-05 10:28:41.000000000 +0800
Change: 2020-11-05 10:28:41.000000000 +0800
--//实际上ctime,mtime都修改了。
$ touch --time=ctime z6.txt
touch: invalid argument `ctime' for `--time'
Valid arguments are:
- `atime', `access', `use'
- `mtime', `modify'
Try `touch --help' for more information.