[20201105]mtime, ctime and atime in Linux.txt

lfree發表於2020-11-05

[20201105]mtime, ctime and atime in Linux.txt

--//大家都知道linux 檔案系統的檔案存在3個時間mtime, ctime and atime。簡單解析如下:
--//

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.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2732288/,如需轉載,請註明出處,否則將追究法律責任。