Linux文件时间属性,Touch命令和文件按时间排序

 

 

本文是《鸟哥的私房菜(第二版)》的笔记和小研究。

 

 

 Linux文件时间属性和Touch命令

鸟哥书中的话:

我们在 ls 这个命令的介绍时,有稍微提到每个文件在linux底下都会记录许多的时间参数,其实是有三个主要的变动时间,那么三个时间的意义是什么呢?

  • modification time (mtime)
    当该文件的『内容数据』变更时,就会升级这个时间!内容数据指的是文件的内容,而不是文件的属性或权限喔!

  • status time (ctime)
    当该文件的『状态 (status)』改变时,就会升级这个时间,举例来说,像是权限与属性被更改了,都会升级这个时间啊。 

  • access time (atime)
    当『该文件的内容被取用』时,就会升级这个读取时间 (access)。举例来说,我们使用 cat 去读取 /etc/man.config , 就会升级该文件的 atime 了。

 

[root@www ~]# touch [-acdmt] 文件
选项与参数:
-a:仅修订 access time;
-c:仅修改文件的时间,若该文件不存在则不创建新文件;
-d:后面可以接欲修订的日期而不用目前的日期,也可以使用 --date="日期或时间"
-m:仅修改 mtime ;
-t:后面可以接欲修订的时间而不用目前的时间,格式为[YYMMDDhhmm]

 

 

 

 

鸟哥关于touch命令的部分参数有点小问题。我这里用touch命令分别修改三个不同的文件,来看看文件时间。

 

touch新建123三个文件,文件时间如下:

[root@mail test]#stat 1;stat 2;stat 3

File: `1'

Size: 0        Blocks: 0IO Block: 4096regular empty file

Device: fd00h/64768d        Inode: 1474935Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:12:05.000000000 +0800

Modify: 2013-06-08 15:12:05.000000000 +0800

Change: 2013-06-08 15:12:05.000000000 +0800

File: `2'

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

Device: fd00h/64768d        Inode: 1475019Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:15:26.000000000 +0800

Modify: 2013-06-08 15:15:26.000000000 +0800

Change: 2013-06-08 15:15:26.000000000 +0800

File: `3'

Size: 0        Blocks: 0IO Block: 4096regular empty file

Device: fd00h/64768d        Inode: 1474938Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:12:11.000000000 +0800

Modify: 2013-06-08 15:12:11.000000000 +0800

Change: 2013-06-08 15:12:11.000000000 +0800

 

 

  • touch -c命令会修改文件3个时间

 

[root@mail test]#touch -c 1

[root@mail test]#stat 1

File: `1'

Size: 0        Blocks: 0IO Block: 4096regular empty file

Device: fd00h/64768d        Inode: 1474935Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:23:34.000000000 +0800

Modify: 2013-06-08 15:23:34.000000000 +0800

Change: 2013-06-08 15:23:34.000000000 +0800

 

  • touch -a命令会修改文件的atimectime

[root@mail test]#touch -a 2

[root@mail test]#stat 2

File: `2'

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

Device: fd00h/64768d        Inode: 1475019Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:24:53.000000000 +0800

Modify: 2013-06-08 15:15:26.000000000 +0800

Change: 2013-06-08 15:24:53.000000000 +0800

 

 

  • touch -m命令会修改文件的mtimectime

[root@mail test]#touch -m 3

[root@mail test]#stat 3

File: `3'

Size: 0        Blocks: 0IO Block: 4096regular empty file

Device: fd00h/64768d        Inode: 1474938Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:12:11.000000000 +0800

Modify: 2013-06-08 15:34:22.000000000 +0800

Change: 2013-06-08 15:34:22.000000000 +0800

 

所以说,touch -m和touch -a命令各自都会修改ctime,并且再修改对应的mtime 和atime。个人觉得,直接用change time解释成ctime(状态修改时间)更好点。

 

 

 

文件的按时间排序

 

  • 先看帮助文档关于时间排序的部分

 

[root@mail ~]#man ls | grep time

-cwith -lt: sort by, and show, ctime (time of last modification of

file status information) with -l: show ctime andsortbyname

otherwise: sort by ctime

extension-X, none -U, size -S, time -t, version -v, status -c,

time -t, atime -u, access -u, use -u

with -l, show time as WORD instead of modification time:atime,

access,use, ctime or status; use specified time as sort key if

with -l, show times using style STYLE: full-iso, long-iso,iso,

-tsort by modification time

-uwith -lt: sort by, and show, access time with-l:showaccess

time and sort by name otherwise: sort by access time

 

 

 

  • 3个文件的各自时间:

 

[root@mail test]#stat 1;stat 2;stat 3

File: `1'

Size: 0        Blocks: 0IO Block: 4096regular empty file

Device: fd00h/64768d        Inode: 1474935Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:23:34.000000000 +0800

Modify: 2013-06-08 15:23:34.000000000 +0800

Change: 2013-06-08 15:23:34.000000000 +0800

File: `2'

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

Device: fd00h/64768d        Inode: 1475019Links: 1

Access: (0644/-rw-r--r--)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:24:53.000000000 +0800

Modify: 2013-06-08 15:15:26.000000000 +0800

Change: 2013-06-08 15:24:53.000000000 +0800

File: `3'

Size: 0        Blocks: 0IO Block: 4096regular empty file

Device: fd00h/64768d        Inode: 1474938Links: 1

Access: (0777/-rwxrwxrwx)Uid: (0/root)Gid: (0/root)

Access: 2013-06-08 15:12:11.000000000 +0800

Modify: 2013-06-08 15:34:22.000000000 +0800

Change: 2013-06-08 15:53:58.000000000 +0800

 

  • 开始练习:

 

ls -tls -lt排序出来的结果一致:

[root@mail test]#ls -t

312

[root@mail test]#ls -lt

total 4

-rwxrwxrwx 1 root root 0 Jun8 15:34 3

-rw-r--r-- 1 root root 0 Jun8 15:23 1

-rw-r--r-- 1 root root 9 Jun8 15:15 2

 

 

 

但是用ls -uls -lu排序结果就不一致了

[root@mail test]#ls -u

213

[root@mail test]#ls -lu

total 4

-rw-r--r-- 1 root root 0 Jun8 15:23 1

-rw-r--r-- 1 root root 9 Jun8 15:24 2

-rwxrwxrwx 1 root root 0 Jun8 15:12 3

 

再加上-t参数,排序正常了

[root@mail test]#ls -ltu

total 4

-rw-r--r-- 1 root root 9 Jun8 15:24 2

-rw-r--r-- 1 root root 0 Jun8 15:23 1

-rwxrwxrwx 1 root root 0 Jun8 15:12 3

 

在看看用ctime排序,加-t参数的影响

[root@mail test]#ls -ltc

total 4

-rwxrwxrwx 1 root root 0 Jun8 15:53 3

-rw-r--r-- 1 root root 9 Jun8 15:24 2

-rw-r--r-- 1 root root 0 Jun8 15:23 1

[root@mail test]#ls -lc

total 4

-rw-r--r-- 1 root root 0 Jun8 15:23 1

-rw-r--r-- 1 root root 9 Jun8 15:24 2

-rwxrwxrwx 1 root root 0 Jun8 15:53 3

 

之前,没看懂帮助文档的意思。现在明白了。文档是指,ls -lt是一个按时间排序的基础命令,他会按mtime排序文件。在这命令之后加上-c参数,即ls -ltc可以按ctime排序。同理ls -ltu则是按atime排序。单纯的ls -lc只是列出文件的ctime,但按文件名排序。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章