元数据定义
MetaData元数据是描述一个文件的特征的系统数据,诸如访问权限、文件拥有者以及文件数据。
在集群文件系统中,也会看到DateNode和MetaNode,后者就是描述文件在集群中的位置。
不论在单机环节或集群环境,用户操作一个文件前,必须首先得到它的元数据,才能定位到文件的位置并且得到文件的内容或相关属性。

查看命令:stat

[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1579      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 13:25:00.903142426 +0800
Modify: 2020-04-22 13:24:57.440986257 +0800
Change: 2020-04-22 13:24:57.441986302 +0800
  • File:文件名称
  • Size:文件大小
  • Blocks:占据的磁盘块数
  • IO Block:IO块大小
  • Rregular file:普通文件
  • Device:所在设备
  • Inode:Inode节点号
  • Links:被硬链接次数
  • Access:访问权限
  • Uid:uid号和属主
  • Gid:gid号和属组
  • Access: 文件最近一次被访问的时间
  • Modify: 文件最近一次被修改的时间
  • Change:元数据最近一次被修改的时间
    备注:
  • 时间字段是完整的年月日、时分秒 以及纳秒数和时区
  • 通过以下小实验求证其正确的定义,依靠date +%N打印出纳秒数
  • 因为已经是极致,即使通过命令让以下命令“几乎同时运行”,仍然存在滞后
[root@oracle tmp]# touch test10; stat test10 ; echo "Nowtime" "`date +%F" "%T.%N" "%z`"
  File: ’test10'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 2228238     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 18:11:59.903022404 +0800
Modify: 2020-04-22 18:11:59.903022404 +0800
Change: 2020-04-22 18:11:59.903022404 +0800
Nowtime 2020-04-22 18:11:59.914273670 +0800

如何修改文件的时间戳信息?
命令touch可以同时刷新元数据的时间戳信息

File: 'profile'
  Size: 1827      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 21:12:37.165113482 +0800
Modify: 2020-04-22 21:13:47.816084697 +0800
Change: 2020-04-22 21:21:30.586551224 +0800
[root@oracle tmp]# touch profile
[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1827      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 21:26:33.237331074 +0800
Modify: 2020-04-22 21:26:33.237331074 +0800
Change: 2020-04-22 21:26:33.237331074 +0800

或者利用touch的参数修改指定时间戳的信息:
touch命令 格式:touch [OPTION]… FILE…
-a 仅改变 atime和ctime
-m 仅改变 mtime和ctime
-t [[CC]YY]MMDDhhmm[.ss]
指定atime和mtime的时间戳

不同的文件操作如何影Metadata?

mv操作 影响ctime和inode (Device可能会更新 )

  • 移动前
[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1579      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 13:25:00.903142426 +0800
Modify: 2020-04-22 13:24:57.440986257 +0800
Change: 2020-04-22 13:24:57.441986302 +0800
  • 移动后
[root@oracle tmp]# mv profile /home/
[root@oracle tmp]# ls /home/profile 
/home/profile
[root@oracle tmp]# stat /home/profile 
  File: '/home/profile'
  Size: 1579      	Blocks: 8          IO Block: 4096   regular file
Device: fd02h/64770d	Inode: 881         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 13:25:00.903142426 +0800
Modify: 2020-04-22 13:24:57.440986257 +0800
Change: 2020-04-22 17:29:29.229779538 +0800

创建链接 影响 ctime和Links

[root@oracle tmp]# stat profile 
 File: 'profile'
Size: 1823      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 17:33:26.150897074 +0800
Modify: 2020-04-22 17:33:26.151897121 +0800
Change: 2020-04-22 17:33:26.151897121 +0800
[root@oracle tmp]# ln profile profile.link
[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1823      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 17:33:26.150897074 +0800
Modify: 2020-04-22 17:33:26.151897121 +0800
Change: 2020-04-22 17:33:41.302607186 +0800

echo 重定向 影响mtime和ctime(size、Blocks随着内容的增加会放大)

[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1823      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 17:33:26.150897074 +0800
Modify: 2020-04-22 17:33:26.151897121 +0800
Change: 2020-04-22 17:33:41.302607186 +0800
[root@oracle tmp]# echo a >> profile
[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1825      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 17:33:26.150897074 +0800
Modify: 2020-04-22 21:11:06.772137866 +0800
Change: 2020-04-22 21:11:06.772137866 +0800

vi不修改内容后退出 只会影响atime
vi修改内容后退出 影响mtime和ctime (size、Blocks随着内容的增加会放大)

[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1825      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 21:12:37.165113482 +0800
Modify: 2020-04-22 21:11:06.772137866 +0800
Change: 2020-04-22 21:11:06.772137866 +0800
[root@oracle tmp]# vi profile
[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1827      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 21:12:37.165113482 +0800
Modify: 2020-04-22 21:13:47.816084697 +0800
Change: 2020-04-22 21:13:47.818084782 +0800

拷贝操作 影响atime

[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1827      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 21:26:33.237331074 +0800
Modify: 2020-04-22 21:26:33.237331074 +0800
Change: 2020-04-22 21:26:33.237331074 +0800
[root@oracle tmp]# cp profile profile.bakbak
[root@oracle tmp]# stat profile
  File: 'profile'
  Size: 1827      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228228     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-22 21:35:38.026422960 +0800
Modify: 2020-04-22 21:26:33.237331074 +0800
Change: 2020-04-22 21:26:33.237331074 +0800

小结:

  1. mtime 仅当修改文件内容时才会更新,更准确地说是修改了文件的数据块 data block部分;
  2. ctime 仅当修改文件属性时才会更新,更准确地说是修改了文件的元数据 inode block 部分,例如 重命名文件、修改文件所有者、移动文件等。
  3. atime 当文件被访问(比如cp拷贝命令、修改文件vi命令等)时就会得到更新
  4. 修改文件内容,必然更新mtime,也会更新ctime。因为修改文件内容时至少已经修改了inode记录上的mtime,这也属于元数据的部分。
  5. 针对atime的追踪意义不是很大,主要是基于性能考虑。假设每次读取文件时都即时更新atime就会带来对磁盘额外的写操作,增加系统负担没,没有必要每次更新atime,从CentOS6开始,默认开启relatime选项要求文件atime只有满足两个条件之一才更新:
    条件1 文件的atime超过一天以上
    条件2 文件的mtime比atime更晚