本文是《鳥哥的私房菜(第二版)》的筆記和小研究。
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新建1、2、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: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命令會修改文件的atime和ctime
[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命令會修改文件的mtime和ctime
[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 -t和ls -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 -u和ls -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,但按文件名排序。