`

Linux文件的access time,change time,modify time

 
阅读更多

两者有此不同,在Linux下没有创建时间的概念 ,也就是不能知道文件的建立时 间,但如果文件建立后就没有修改过,修改时间=建立时间;如果文件建立后,状态就没有改动过,那么状态改动时间=建立时间;如果文件建立后,没有被读取 过,那么访问时间=建立时间,因为不好判断文件是否被改过、读过、其状态是否变过,所以判断文件的建立时间基本上能为不可能。

 
如何查一个文件的三个时间呢?
先用下面的命令来建立一个文件
# date && echo "this is file be used test time of file" >filetime.txt && ll --full-time filetime.txt
Tue Aug  4 15:13:44 HKT 2009
-rw-r--r--    1 root     root           39 2009-08-04 15:13:44.000000000 +0800 filetime.txt
 
通过stat filename.txt来查, 如:
# stat filetime.txt 
  File: `filetime.txt'
  Size: 39              Blocks: 8          IO Block: 4096   Regular File
Device: 802h/2050d      Inode: 17          Links: 1    
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-08-04 15:13:44.000000000 +0800
Modify: 2009-08-04 15:13:44.000000000 +0800
Change: 2009-08-04 15:13:44.000000000 +0800


说明:Access访问时间。Modify修改时间。Change状态改动时间。可以stat *查看这个目录所有文件的状态。

ctime=change time
atime=access time
mtime=modifiy time
 
因为这是一个新的文件(filetime.txt),没做过内容、属性的更改,也没读过这个文件,所以三者(访问时间、修改时间、状态改动时间)的时间是一致的,这时文件的修改时间与这三个时间是一样的,是没有疑问的。
 
1、访问时间,读一次这个文件的内容,这个时间就会更新。比如对这个文件运用 more、cat等命令。ls、stat命令都不会修改文件的访问时间。
2、修改时间,修改时间是文件内容最后一次被修改时间。比如:vi后保存文件。ls -l列出的时间就是这个时间。
3、状态改动时间。是该文件的i节点最后一次被修改的时间,通过chmod、chown命令修改一次文件属性,这个时间就会更新。
 
另个除了可以通过stat来查看文件的mtime,ctime,atime等属性,也可以通过ls命令来查看,具体如下:
ls -lc filename 列出文件的 ctime (最后更改时间)
ls -lu filename 列出文件的 atime(最后存取时间)

 

ls -l filename 列出文件的 mtime (最后修改时间)
 
在linux中stat函数中,用st_atime表示文件数据最近的存取时间(last accessed time);用st_mtime表示文件数据最近的修改时间(last modified time);使用st_ctime表示文件i节点数据最近的修改时间(last i-node's status changed time)。
 
 字段           说明                  例子           ls(-l)
 st_atime  文件数据的最后存取时间       read            -u
 st_mtime  文件数据的最后修改时间       write           缺省
 st_ctime  文件数据的最后更改时间       chown,chmod     -c
 
 
在linux系统 中,系统把文件内容数据与i节点数据是分别存放的,i节点数据存放了文件权限与文件属主之类的数据。
 
另外,可以格式化输出文件的三种时间,如:
find . -name file -printf "%AY-%Am-%Ad %AH:%AM:%AS"
find . -name file -printf "%TY-%Tm-%Td %TH:%TM:%TS"
find . -name file -printf "%CY-%Cm-%Cd %CH:%CM:%CS"
 
linux的ctime代表的是文件修改时间,如果文件被修改过就很难知道文件的创建时间,在某些特殊情况下,需要查看文件的创建时间,正常情况下查看文件的ctime是无法实现的。可以使用一个变通的方法来实现保留文件创建时间,但是同时也会牺牲一些其它特性。
 
可以在mount文件的时候使用参数-o noatime,来把系统更新atime的特性关闭。使用了noatime参数挂载后,在文件被修改后文件的atime是不会被改变的,使用stat查看到的atime就是文件的创建时间。
如:
# / sbin/mkfs -t ext3 /dev/ram10
# mount -t ext3 -o noatime /dev/loop0 /mnt/foo
# mount
/dev/ram10 on /mnt/foo type ext3 (rw,noatime)
# cd /mnt/foo
# touch filetime1.txt
# stat filetime1.txt 
  File: `filetime1.txt'
  Size: 0               Blocks: 0          IO Block: 4096   Regular File
Device: 10ah/266d       Inode: 12          Links: 1    
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-08-04 20:51:32.000000000 +0800
Modify: 2009-08-04 20:51:32.000000000 +0800
Change: 2009-08-04 20:51:32.000000000 +0800
 
# echo foo.ok >> filetime1.txt 
[root@logs-bak foo]# stat filetime1.txt 
  File: `filetime1.txt'
  Size: 14              Blocks: 2          IO Block: 4096   Regular File
Device: 10ah/266d       Inode: 12          Links: 1    
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-08-04 20:51:32.000000000 +0800
Modify: 2009-08-04 20:53:27.000000000 +0800
Change: 2009-08-04 20:53:27.000000000 +0800
 
# cat filetime1.txt 
  foo.ok
#stat filetime1.txt 
  File: `filetime1.txt'
  Size: 14              Blocks: 2          IO Block: 4096   Regular File
Device: 10ah/266d       Inode: 12          Links: 1    
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-08-04 20:51:32.000000000 +0800
Modify: 2009-08-04 20:53:27.000000000 +0800
Change: 2009-08-04 20:53:27.000000000 +0800
 
# 通过以上实验可以看出文件的access time 是不变的。
 
接着向下多测试一下.
# vi filetime1.txt
# stat filetime1.txt 
  File: `filetime1.txt'
  Size: 23              Blocks: 2          IO Block: 4096   Regular File
Device: 10ah/266d       Inode: 14          Links: 1    
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-08-04 20:55:05.000000000 +0800
Modify: 2009-08-04 20:55:05.000000000 +0800
Change: 2009-08-04 20:55:05.000000000 +0800
 
# chmod 777 filetime1.txt 
# stat filetime1.txt 
  File: `filetime1.txt'
  Size: 23              Blocks: 2          IO Block: 4096   Regular File
Device: 10ah/266d       Inode: 14          Links: 1    
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-08-04 20:55:05.000000000 +0800
Modify: 2009-08-04 20:55:05.000000000 +0800
Change: 2009-08-04 20:57:36.000000000 +0800
可见,chmod后,Ctime的变化。
#########################################################
说到这里,大概大家也对在linux下文件的时间有所了解了吧!
那么以下的操作对文件的时间有什么影响呢?
 
操作                        atime        mtime       ctime
----------------------------------------------------------
mv
----------------------------------------------------------
cp
----------------------------------------------------------
touch
----------------------------------------------------------
cat/more/less
----------------------------------------------------------
ls
----------------------------------------------------------
chmod/chown
----------------------------------------------------------
ln
----------------------------------------------------------
echo
----------------------------------------------------------
vi
----------------------------------------------------------
(请大家测试完成这个表吧!!!!做完测试可以给我留言啊!)
##############################################################
 
另外,从kernel2.6.29开,还默认集成了一个relatime的属性。可能是因为在文件读操作很频繁的系统
中,atime更新所带来的开销很大,所以很多SA都在挂装文件系统的时候使用noatime属性来停止更新atime。但是有些程序需要根据atime进行一些判断和操作,所以Linux就推出了一个relatime特性。
使用这个特性来挂装文件系统后,只有当mtime比atime更新的时候,才会更新atime。事实上,这个时候atime和mtime已经是同一个东西 了。所以这个选项就是为了实现对atime的兼容才推出的。并不是一个新的时间属性。使用方法就是通过mount -o relatime /dir来挂装目录。
分享到:
评论

相关推荐

    浅谈find命令下的atime,ctime,mtime

    atime:access time,最近一次访问时间,当使用这个文件的时候就会更新这个时间。 mtime:modify time,最近一次文件内容更改时间,当修改文件内容数据时,就会更改这个时间。 ctime:change time,最近一次文件属性或...

    8-07-14_MegaCLI for linux_windows

    Linux Solaris Windows 分别对应如下目录 MegaCLI for DOS MegaCLI for Linux MegaCLI for Solaris MegaCLI for FreeBSD MegaCLI for Windows ********************************************* LSI Corp. Mega...

    cutEditor -最好用的在线编辑器

    Create and modify tables and table cells. Set their border color, alignment, cellspacing and more! Once you've created a table, simply right click inside of it and use the handy popup menu to change ...

    UE(官方下载)

    Parsing XML can be a time-consuming task, especially when large amounts of data are involved. As of v15.10, UltraEdit provides you with a the XML Window for the purpose of parsing ...

    计算机网络第六版答案

    28. Trudy can pretend to be Bob to Alice (and vice-versa) and partially or completely modify the message(s) being sent from Bob to Alice. For example, she can easily change the phrase “Alice, I owe ...

    Universal-USB-Installer

    安装linux工具源码 (UUI) Universal USB Installer ?009-2012 Lance http://www.pendrivelinux.com This Open Source tool falls under the GNU General Public License Version 2 Source Code is made available at...

    cuteEditor6.0

    This can result in web pages that take an unnecessarily long time to download. The Paste from Word button solves this by removing word markup before pasting the text into your page. 支持...

    WizFlow网页编辑

    operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a ...

    hibernate-shards.jar

    operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a ...

    Bochs - The cross platform IA-32 (x86) emulator

    - Fix MTRR configuration (prevented boot of modern Linux kernels) - Fix interrupt vectors for INT 60h-66h (reserved for user interrupt) by setting them to zero - Fix BIOS INT13 function 08 when ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...

    grub4dos-V0.4.6a-2017-02-04更新

    --timeout=[x]=[y]=[color] 倒计时位置、颜色。单位:列,行,24位色彩。 2015-05-14(yaya) 改进 NTFS 文件系统: 对于驻留属性文件(小文件),可以写,也可用 blocklist 显示信息。 对于非驻留属性列表,...

    acpi控制笔记本风扇转速

    Change for GPE support: when a "wake" GPE is received, all wake GPEs are now immediately disabled to prevent the waking GPE from firing again and to prevent other wake GPEs from interrupting the wake ...

    DebuggingWithGDB 6.8-2008

    Table of Contents Summary of gdb . . . . . . . . ....Free Software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....

Global site tag (gtag.js) - Google Analytics