Linux下时间戳解疑(20180522)

今天调测一个版本:”程序模块写入一个文件的过程中,同时shell脚本定时会对文件扫描做处理“,在调测过程中,发现程序模块在操作大数据的情况会,会分批次写入文件,而shell脚本会在这期间将文件处理掉,导致文件不完整。

处理上述问题的方法,也比较简单,shell脚本中扫描文件的部分做了下处理,通过find命令扫描mmin在10分钟外的文件(find . -mmin +10)。

问题比较简单,但是引发了对linux时间戳(atime、ctime、mtime)三者的疑惑,遂赶紧查资料研究了一把。

一、借用find的man介绍,理解三个概念

       -atime n
              File was last accessed n*24 hours ago.  When find figures out how many 24-hour periods ago the file was
              last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been  accessed
              at least two days ago.
      -ctime n
              File's  status was last changed n*24 hours ago.  See the comments for -atime to understand how rounding
              affects the interpretation of file status change times.
       -mtime n
              File's data was last modified n*24 hours ago.  See the comments for -atime to understand  how  rounding
              affects the interpretation of file modification times.

即:atime,代表accessed time,访问时间;ctime,代表changed time,变动时间;mtime,代表modified time,修改时间;

二、试验时间

step1

MicroWin10-1123:/mnt/c/Users/work/test # echo "hello" > filetimestamp
MicroWin10-1123:/mnt/c/Users/work/test # stat filetimestamp
  File: 'filetimestamp'
  Size: 6               Blocks: 0          IO Block: 512    regular file
Device: ch/12d  Inode: 4222124650666898  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-05-22 23:46:56.467926700 +0800
Modify: 2018-05-22 23:46:56.467926700 +0800
Change: 2018-05-22 23:46:56.467926700 +0800
 Birth: -

注:新建文件的过程中,atime\ctime\mtime均保持一致。

step2

MicroWin10-1123:/mnt/c/Users/work/test # cat filetimestamp
hello
MicroWin10-1123:/mnt/c/Users/work/test # stat filetimestamp
  File: 'filetimestamp'
  Size: 6               Blocks: 0          IO Block: 512    regular file
Device: ch/12d  Inode: 4222124650666898  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-05-22 23:47:07.802327400 +0800
Modify: 2018-05-22 23:46:56.467926700 +0800
Change: 2018-05-22 23:46:56.467926700 +0800
 Birth: -
注:cat、more、vi等操作会更新文件的atime(文件为空的情况下不变)

step3

MicroWin10-1123:/mnt/c/Users/work/test # echo "hello2" > filetimestamp
MicroWin10-1123:/mnt/c/Users/work/test # stat filetimestamp
  File: 'filetimestamp'
  Size: 7               Blocks: 0          IO Block: 512    regular file
Device: ch/12d  Inode: 4503599627377766  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-05-22 23:50:43.608248700 +0800
Modify: 2018-05-22 23:50:43.608248700 +0800
Change: 2018-05-22 23:50:43.608248700 +0800
 Birth: -

注:文件内容变更,会同时更新atime\ctime\mtime。

step4

MicroWin10-1123:/mnt/c/Users/work/test # mv filetimestamp files
MicroWin10-1123:/mnt/c/Users/work/test # stat files
  File: 'files'
  Size: 7               Blocks: 0          IO Block: 512    regular file
Device: ch/12d  Inode: 4503599627377766  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-05-22 23:50:43.608248700 +0800
Modify: 2018-05-22 23:50:43.608248700 +0800
Change: 2018-05-22 23:52:13.828520800 +0800
 Birth: -

注:重命名操作,会更新ctime

step5

MicroWin10-1123:/mnt/c/Users/work/test # chmod 700 files
MicroWin10-1123:/mnt/c/Users/work/test # stat files
  File: 'files'
  Size: 7               Blocks: 0          IO Block: 512    regular file
Device: ch/12d  Inode: 4503599627377766  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-05-22 23:50:43.608248700 +0800
Modify: 2018-05-22 23:50:43.608248700 +0800
Change: 2018-05-22 23:52:13.828520800 +0800
 Birth: -

注:变更权限,ctime没变化




你可能感兴趣的:(Linux下时间戳解疑(20180522))