เรื่องของ atime mtime และ ctime ของไฟล์บน Unix

บางครั้งเราเอาไฟล์ไปตั้งไว้บน Server นานๆ จนลืมไป แล้วต้องการรู้ว่าเราตั้งไว้นานแล้วหรือยัง และถูกเปลี่ยนไปเมื่อไหร เก่า หรือใหม่อย่างไร
อีกประการ ยังถูกใช้โดยโปรแกรม Backup เพื่อเลือกไฟล์สำหรับการทำ Backup แบบ Incremental คือ เลือกเฉพาะไฟล์ที่เพิ่มขึ้นใหม่ หรือไฟล์ที่ถูกเปลี่ยนแปลงเท่านั้น
เวลาของไฟล์บน Unix มีอยู่ 3 แบบ คือ atime, ctime, mtime ในที่นี้จะอธิบายการเปลี่ยนแปลงของค่าเวลาต่างๆ ดังนี้

ใช้คำสั่ง date เพื่อดูเวลาที่ทดสอบ

[root@SkyIT /time]$ date
Wed Jan 25 16:42:41 ICT 2012

สร้างไฟล์เปล่าๆ ด้วยคำสั่ง touch

[root@SkyIT /time]$ touch test1.txt

ใช้คำสั่ง ls -l เพื่อดูข้อมูลไฟล์เบื้องต้น

[root@SkyIT /time]$ ls -l test1.txt
-rw-r--r-- 1 root root 0 Jan 25 17:02 test1.txt

ใช้คำสั่ง stat เพื่อดูข้อมูลไฟล์โดยละเอียด

[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 0               Blocks: 0          IO Block: 8192   regular empty file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:02:21.684449000 +0700
Modify: 2012-01-25 17:02:21.684449000 +0700
Change: 2012-01-25 17:02:21.684455000 +0700

หมายเหตุ เวลาที่แสดงจากคำสั่ง ls -l เป็นเวลา Modify (mtime)

เวลา Access (atime)

เมื่อไฟล์ถูก access หรือดูเนื้อหาภายในไฟล์ เวลา Access (atime) จะถูกปรับปรุงโดยอัตโนมัติ

เช่น ใช้คำสั่ง cat เพื่อเรียกดูไฟล์ เวลา Access (atime) ก็จะถูกเปลี่ยนไป ตามเวลาที่ access

[root@SkyIT /time]$ date
Wed Jan 25 17:04:43 ICT 2012
[root@SkyIT /time]$ cat test1.txt
[root@SkyIT /time]$ ls -l test1.txt
-rw-r--r-- 1 root root 0 Jan 25 17:02 test1.txt
[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 0               Blocks: 0          IO Block: 8192   regular empty file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:04:59.253304000 +0700
Modify: 2012-01-25 17:02:21.684449000 +0700
Change: 2012-01-25 17:02:21.684455000 +0700

สังเกตว่า เวลาของไฟล์ จากการรันคำสั่ง ls -l ไม่เปลี่ยนแปลง เพราะ mtime ไม่เปลี่ยน

เวลา Change (ctime)

เมื่อมีการเปลี่ยนแปลงข้อมูลเกี่ยวกับ Inode ของไฟล์ เช่นการเปลี่ยน Owner, Group, Permission เวลา ctime จะถูกปรับปรุง

ตัวอย่างการเปลี่ยน permission ทำให้เวลา ctime เปลี่ยนแปลง

[root@SkyIT /time]$ date
Wed Jan 25 17:05:55 ICT 2012
[root@SkyIT /time]$ chmod 600 test1.txt
[root@SkyIT /time]$ ls -l test1.txt
-rw------- 1 root root 0 Jan 25 17:02 test1.txt
[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 0               Blocks: 0          IO Block: 8192   regular empty file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:04:59.253304000 +0700
Modify: 2012-01-25 17:02:21.684449000 +0700
Change: 2012-01-25 17:06:06.641615000 +0700

สังเกตว่า เวลา Access (atime), Modify (mtime) หรือเวลาจากคำสั่ง ls -l ไม่เปลี่ยนแปลงไป เพราะคำสั่ง chmod เปลี่ยนแค่ข้อมูลใน Inode

เวลา Modify (mtime)

เมื่อเนื้อหาในไฟล์ถูกแก้ไข เวลา Modify (mtime) จะถูกปรับปรุง ตัวอย่างเช่น

[root@SkyIT /time]$ date
Wed Jan 25 17:08:37 ICT 2012

ใช้คำสั่ง echo และ shell redirection “>” เพิ่มเนื้อหาเข้าไปในไฟล์

[root@SkyIT /time]$ echo "add more content to file" >> test1.txt
[root@SkyIT /time]$ ls -l test1.txt
-rw------- 1 root root 25 Jan 25 17:09 test1.txt
[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 25              Blocks: 2          IO Block: 8192   regular file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:04:59.253304000 +0700
Modify: 2012-01-25 17:09:24.682162000 +0700
Change: 2012-01-25 17:09:24.682162000 +0700

เมื่อเนื้อหาในไฟล์ถูกเปลี่ยน เวลา Change (ctime) ก็จะถูกปรับไปด้วย

Comments are closed.