为什么有时候读取文件,atime不更新

简介:

在linux中,使用stat foo.txt 命令可以看到文件foo.txt的三个时间:

atime:access time,访问时间

mtime:modify time,修改时间,文件内容有修改

ctime:change time,create time,改变时间,文件的索引节点发生变化,具体的情况有:1、文件内容有修改;2、文件权限有修改;3、inode变了;4、重命名(重命名不会导致inode改变)

PS:

1、如果用vi去修改某个文件,可能会发现这三个时间都被更新了,因为vi使用了临时文件保存修改,在wq时替换了原来的文件,导致文件的inode被改变了,可以用ls -li验证一下

2、如果想修改mtime,可以echo "hello world" >> foo.txt,注意ctime也会跟着改变

3、如果想仅仅修改ctime,可以chmod 644 foo.txt,mtime不会改变

4、为什么没说atime呢,不是想象中的那么简单的,后面详细分析

 

对于一个文件foo.txt

ls -l foo.txt 显示的是mtime

ls -l -c foo.txt 显示的是ctime

ls -l -u foo.txt 显示的是atime

 

对于atime,当你以为cat foo.txt然后stat foo.txt能看到atime改变的话,很可能就会失望了,并不是每次atime都会改变的

atime和mount的参数以及内核有关:

 

       atime  Do  not  use noatime feature, then the inode access time is controlled
              by kernel defaults. See  also  the  description  for  strictatime  and
              relatime mount options.

       noatime
              Do  not update inode access times on this filesystem (e.g., for faster
              access on the news spool to speed up news servers).
       relatime
              Update inode access times relative to modify or change  time.   Access
              time  is only updated if the previous access time was earlier than the
              current modify or change time. (Similar to noatime, but doesn't  break
              mutt  or  other applications that need to know if a file has been read
              since the last time it was modified.)

              Since Linux 2.6.30, the kernel defaults to the  behavior  provided  by
              this  option  (unless  noatime  was   specified),  and the strictatime
              option is required to obtain traditional semantics. In addition, since
              Linux  2.6.30,  the file's last access time is always  updated  if  it
              is more than 1 day old.

       norelatime
              Do not use relatime feature. See also the strictatime mount option.

       strictatime
              Allows to explicitly requesting full atime updates. This makes it pos‐
              sible  for  kernel  to defaults to relatime or noatime but still allow
              userspace to override it. <span style="color:#ff0000;">For more details about  the  default  system
              mount options see /proc/mounts</span>.

       nostrictatime
              Use the kernel's default behaviour for inode access time updates.

如果使用noatime,那么atime就不会被更新,即使修改了文件内容

如果使用atime,采用内核默认行为,kernel2.6.30后就相当于使用了relatime

如果使用relatime,表示当atime比ctime或mtime更早,然后你又去读取了文件,atime才会被更新为当前时间,kernel2.6.30后的默认行为;或者atime比现在早一天,那么atime在文件读取时会被更新

如果使用strictatime,atime在文件每次被读取时,都能够被更新

cat /proc/mounts可以看到我的服务器使用的是relatime参数: 

/dev/sdl1 /home ext4 rw,relatime,user_xattr,barrier=1,data=ordered 0 0

  

实验环节:

noatime,可以看到不管是修改文件还是读取文件,atime都不会变化,性能最好

shuyin.wsy@localhost:~$ sudo mount -t tmpfs -o noatime tmpfs /mnt
shuyin.wsy@localhost:~$ cd /mnt
shuyin.wsy@localhost:/mnt$ echo "hello world" >> foo.c
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: 18h/24d Inode: 60855528    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:46:19.734162324 +0800 #最初值
Modify: 2016-04-11 17:46:19.734162324 +0800
Change: 2016-04-11 17:46:19.734162324 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ echo "hello world" >> foo.c
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 24              Blocks: 8          IO Block: 4096   regular file
Device: 18h/24d Inode: 60855528    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:46:19.734162324 +0800 #写文件后,atime不变
Modify: 2016-04-11 17:46:38.142096924 +0800
Change: 2016-04-11 17:46:38.142096924 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cat foo.c
hello world
hello world
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 24              Blocks: 8          IO Block: 4096   regular file
Device: 18h/24d Inode: 60855528    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:46:19.734162324 +0800 #读文件后,atime不变
Modify: 2016-04-11 17:46:38.142096924 +0800
Change: 2016-04-11 17:46:38.142096924 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cd
shuyin.wsy@localhost:~$ sudo umount /mnt

 

relatime,当atime早于或等于mtime/ctime时,才会被更新,2.6.30后的内核的默认行为,性能和atime更新折中的选择
shuyin.wsy@localhost:~$ sudo mount -t tmpfs -o relatime tmpfs /mnt
shuyin.wsy@localhost:~$ cd /mnt
shuyin.wsy@localhost:/mnt$ echo "hello world" >> foo.c
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60855680    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:51:44.772736377 +0800 #最初值
Modify: 2016-04-11 17:51:44.772736377 +0800
Change: 2016-04-11 17:51:44.772736377 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cat foo.c > /dev/null
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60855680    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:51:56.036682655 +0800 #atime早于等于mtime/ctime,更新
Modify: 2016-04-11 17:51:44.772736377 +0800
Change: 2016-04-11 17:51:44.772736377 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cat foo.c > /dev/null
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60855680    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:51:56.036682655 +0800 #atime更晚时,不更新
Modify: 2016-04-11 17:51:44.772736377 +0800
Change: 2016-04-11 17:51:44.772736377 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ echo "hello world" >> foo.c
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 24              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60855680    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:51:56.036682655 +0800 #改变文件后,atime不更新
Modify: 2016-04-11 17:52:30.636519093 +0800
Change: 2016-04-11 17:52:30.636519093 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cat foo.c > /dev/null
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 24              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60855680    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 17:52:43.708457830 +0800 #读取文件后,如果atime早于等于mtime/ctime,更新
Modify: 2016-04-11 17:52:30.636519093 +0800
Change: 2016-04-11 17:52:30.636519093 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cd
shuyin.wsy@localhost:~$ sudo umount /mnt

 

relatime,atime比现在早一天,那么atime在文件读取时会被更新,随便找个老文件看看:

shuyin.wsy@localhost:~$ stat elfdiff.sh
  File: `elfdiff.sh'
  Size: 2067      	Blocks: 8          IO Block: 4096   regular file
Device: 8b1h/2225d	Inode: 103158619   Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-08 17:31:36.764025160 +0800 #atime比ctime和mtime晚,且距离现在超过一天
Modify: 2016-04-08 17:31:33.536040622 +0800
Change: 2016-04-08 17:31:33.580040411 +0800
 Birth: -
shuyin.wsy@localhost:~$ cat elfdiff.sh >/dev/null
shuyin.wsy@localhost:~$ stat elfdiff.sh
  File: `elfdiff.sh'
  Size: 2067      	Blocks: 8          IO Block: 4096   regular file
Device: 8b1h/2225d	Inode: 103158619   Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 22:12:31.984860509 +0800 #符合1天的条件,更新atime
Modify: 2016-04-08 17:31:33.536040622 +0800
Change: 2016-04-08 17:31:33.580040411 +0800
 Birth: -

 

strictatime,我们以为的那种atime,每次读取都会更新,但是影响性能
shuyin.wsy@localhost:~$ sudo mount -t tmpfs -o strictatime tmpfs /mnt
shuyin.wsy@localhost:~$ cd /mnt
shuyin.wsy@localhost:/mnt$ echo "hello world" >> foo.c
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60853117    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 18:03:28.033857401 +0800 #最初值
Modify: 2016-04-11 18:03:28.033857401 +0800
Change: 2016-04-11 18:03:28.033857401 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cat foo.c >> /dev/null
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60853117    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 18:03:35.229824129 +0800 #只要被读取,atime就更新
Modify: 2016-04-11 18:03:28.033857401 +0800
Change: 2016-04-11 18:03:28.033857401 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cat foo.c >> /dev/null
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60853117    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 18:03:38.925807067 +0800 #只要被读取,atime就更新
Modify: 2016-04-11 18:03:28.033857401 +0800
Change: 2016-04-11 18:03:28.033857401 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ echo "hello world" >> foo.c
shuyin.wsy@localhost:/mnt$ stat foo.c
  File: `foo.c'
  Size: 24              Blocks: 8          IO Block: 4096   regular file
Device: 19h/25d Inode: 60853117    Links: 1
Access: (0644/-rw-r--r--)  Uid: (122712/shuyin.wsy)   Gid: (  100/   users)
Access: 2016-04-11 18:03:38.925807067 +0800 #改变文件,atime不更新
Modify: 2016-04-11 18:03:44.961779241 +0800
Change: 2016-04-11 18:03:44.961779241 +0800
 Birth: -
shuyin.wsy@localhost:/mnt$ cd
shuyin.wsy@localhost:~$ sudo umount /mnt

注意:当文件夹中的某个文件的时间改变时,文件夹本身的时间并不会受到影响

 
参考:

http://unix.stackexchange.com/questions/88840/atime-value-changing-only-once-after-file-creation

目录
相关文章
|
3月前
|
运维
简记:清理指定后缀名文件的 powerhsell 小脚本
简记:清理指定后缀名文件的 powerhsell 小脚本
18 0
|
5月前
|
Python
python 获取、修改 时间戳 时间属性 文件时间
python 获取、修改 时间戳 时间属性 文件时间
32 0
保存文件的同时删除文件,保存用时会略微升高
保存文件的同时删除文件,保存用时会略微升高
|
C语言
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 逐行遍历文件文本数据 | 获取文件中的文本行 | 查询文本行数据 | 追加文件数据 | 使用占位符方式拼接字符串 )
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 逐行遍历文件文本数据 | 获取文件中的文本行 | 查询文本行数据 | 追加文件数据 | 使用占位符方式拼接字符串 )
219 0
|
C语言
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 函数形参设置 | 确保打开文件成功 | 统计文件大小 )
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 函数形参设置 | 确保打开文件成功 | 统计文件大小 )
132 0
|
C语言
【C 语言】文件操作 ( 使用 fread 和 fwrite 实现二进制文件的拷贝 | stat 统计文件大小 | feof 判定文件结尾 )
【C 语言】文件操作 ( 使用 fread 和 fwrite 实现二进制文件的拷贝 | stat 统计文件大小 | feof 判定文件结尾 )
273 0
【C 语言】文件操作 ( 使用 fread 和 fwrite 实现二进制文件的拷贝 | stat 统计文件大小 | feof 判定文件结尾 )
如何更改文件的"修改时间"?
如何更改文件的"修改时间"?
182 0
|
缓存 Windows
文件夹显示文件或目录损坏且没法读取
 用户反馈说Win7打开Local Settings文件夹提示“位置不可用,拒绝访问"怎么办?Local Settings是Windows系统中的一个隐藏文件,一般存放着和上网有关的历史记录和缓存之类的文件,如果打开Local Settings文件时提示"位置不可用,拒绝访问"下面跟随小编脚步一起看看具体解决方法。
560 0