CentOS中通过stat查看文件的元数据

分类:CentOS教程 阅读:41773 次

CentOS中可以通过stat查看文件的元数据

[baby@xiaoxiao abc]$ stat honey

File: `honey'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 806h/2054d Inode: 131076 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 500/ baby) Gid: ( 500/ baby)

Access: 2015-03-30 10:27:51.851234105 +0800

Modify: 2015-03-30 10:44:58.144226717 +0800

Change: 2015-03-30 10:44:58.146226803 +0800

Access:最后访问时间

Modify:最后修改时间

Change:状态更改时间(由系统维护,无法随意修改)

touch -a :仅更新Access time(同时更新Change为current time)

touch -m:仅更新Modify time(同时更新Change为current time)

touch -c:不创建新文件

touch -t:使用指定的时间更新时间戳(仅更改Access time与Modify time,Change time更新为current time)

例:touch -t [[CC]YY]MMDDhhmm[.ss] filename

[root@xiaoxiao cde]# touch -t 199906121808 honey

[root@xiaoxiao cde]# stat honey

File: `honey'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 806h/2054d Inode: 131076 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 500/ baby) Gid: ( 500/ baby)

Access: 1999-06-12 18:08:00.000000000 +0800

Modify: 1999-06-12 18:08:00.000000000 +0800

Change: 2015-03-30 13:31:40.064221158 +0800

当文件的任何数据改变时(包括文件的数据和元数据),Change会更新,对文件重命名(mv),移动(mv)或者复制时(cp -a),系统都会更新文件的Change time。当文件的内容被修改时Modify和Change会同时更新。当Change time更新后,第一次访问该文件,系统会更新Access time(仅更新Access time),之后对该文件的访问,将不会更新Access time。

[baby@xiaoxiao cde]$ touch honey

[baby@xiaoxiao cde]$ stat honey

File: `honey'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 806h/2054d Inode: 131076 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 500/ baby) Gid: ( 500/ baby)

Access: 2015-03-30 11:24:47.041225726 +0800

Modify: 2015-03-30 11:24:47.041225726 +0800

Change: 2015-03-30 11:24:47.041225726 +0800

[baby@xiaoxiao cde]$ cat honey &> /dev/null

[baby@xiaoxiao cde]$ stat honey

File: `honey'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 806h/2054d Inode: 131076 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 500/ baby) Gid: ( 500/ baby)

Access: 2015-03-30 11:25:16.401228081 +0800

Modify: 2015-03-30 11:24:47.041225726 +0800

Change: 2015-03-30 11:24:47.041225726 +0800

用touch更新时间戳后的第一次访问,Access time更新了,下面是第二次访问之后的元数据,Access time并没有更新。

[baby@xiaoxiao cde]$ cat honey &> /dev/null

[baby@xiaoxiao cde]$ stat honey

File: `honey'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 806h/2054d Inode: 131076 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 500/ baby) Gid: ( 500/ baby)

Access: 2015-03-30 11:25:16.401228081 +0800

Modify: 2015-03-30 11:24:47.041225726 +0800

Change: 2015-03-30 11:24:47.041225726 +0800

tar或者各种压缩命令在解压出来时都会更新文件的Change time,若想要不更改Change time备份文件可以使用dd命令(dd命令在复制单个文件时,会更新时间戳,在备份整个分区时,会保留文件的所有属性),过程如下:

新建一个分区sda10,不用格式化,挂载到/media/sda10。

[root@xiaoxiao cde]# ll /dev/sda10

brw-rw---- 1 root disk 8, 10 Mar 30 12:58 /dev/sda10

[root@xiaoxiao abc]# dd if=/dev/sda6 of=/dev/sda10 & (/dev/sda6挂载在/tmp下)

[1] 5917

[root@xiaoxiao abc]# mount /dev/sda10 /media/sda10/

[root@xiaoxiao cde]# stat /media/sda10/cde/honey /tmp/cde/honey

File: `/media/sda10/cde/honey'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 80ah/2058d Inode: 131076 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 500/ baby) Gid: ( 500/ baby)

Access: 2015-03-30 11:25:16.401228081 +0800

Modify: 2015-03-30 11:24:47.041225726 +0800

Change: 2015-03-30 11:24:47.041225726 +0800

File: `/tmp/cde/honey'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 806h/2054d Inode: 131076 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 500/ baby) Gid: ( 500/ baby)

Access: 2015-03-30 11:25:16.401228081 +0800

Modify: 2015-03-30 11:24:47.041225726 +0800

Change: 2015-03-30 11:24:47.041225726 +0800

两个文件的元数据一模一样。