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没变化




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值