atime, ctime and mtime in Unix filesystems

As you know, Unix filesystems store a number of timestamps for each file. This means that you can use these timestamps to find out when any file or directory was last accessed (read from or written to), changed (file access permissions were changed) or modified (written to).

File and directory timestamps in Unix

Three times tracked for each file in Unix are these:

  • access time - atime
  • change time – ctime
  • modify time – mtime

atime – File Access Time

Access time shows the last time the data from a file was accessed – read by one of the Unix processes directly or through commands and scripts.

ctime – File Change Time

ctime also changes when you change file's ownership or access permissions. It will also naturally highlight the last time file had its contents updated.

mtime – File Modify Time

Last modification time shows time of the last change to file's contents. It does not change with owner or permission changes, and is therefore used for tracking the actual changes to data of the file itself.

Find atime, ctime and mtime with ls

The simplest way to confirm the times associated with a file is to use ls command
Timestamps are shown when using the long-format output of ls command, ls -l:

 
 
  1. ubuntu# ls -l /tmp/file1
  2. -rw-r--r-- 1 greys root 9 2008-04-05 07:10 /tmp/file1

This is the default output of ls -l, which shows you the time of the last file modification – mtime. In our example, file /tmp/file1 was last changed around 7:10am. 
If we want to see the last access time for this file, atime – you need to use -lu options for ls. The output will probably show some later time:

 
 
  1. ubuntu# ls -lu /tmp/file1
  2. -rw-r--r-- 1 greys root 9 2008-04-05 07:27 /tmp/file1

In the example, it's 7:27am. 
Lastly, ls -lc will show you the last time our file was changed, ctime:

 
 
  1. ubuntu# ls -lc /tmp/file1
  2. -rw-r--r-- 1 greys root 9 2008-04-05 07:31 /tmp/file1

To show you how this works, I'll change the ownership of the file and then run the same 3 ls commands to show you that only the ctime had been updated. I run the date command just before doing anything else so that you can compare the times:

 
 
  1. ubuntu# date
  2. Sat Apr 5 07:35:16 IST 2008
  3. ubuntu# chown root /tmp/file1
  4. ubuntu# ls -lc /tmp/file1
  5. -rw-r--r-- 1 root root 9 2008-04-05 07:35 /tmp/file1
  6. ubuntu# ls -lu /tmp/file1
  7. -rw-r--r-- 1 root root 9 2008-04-05 07:27 /tmp/file1
  8. ubuntu# ls -l /tmp/file1
  9. -rw-r--r-- 1 root root 9 2008-04-05 07:10 /tmp/file1

Show atime, ctime and mtime with stat command

In Linux distributions, you will probably find a stat command, which can be used to show all of the times in a more convenient way, and among plenty of other useful information about your file:

 
 
  1. ubuntu# stat /tmp/file1
  2. File: `/tmp/file1'
  3. Size: 9 Blocks: 8 IO Block: 4096 regular file
  4. Device: 811h/2065d Inode: 179420 Links: 1
  5. Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
  6. Access: 2008-04-05 07:27:51.000000000 +0100
  7. Modify: 2008-04-05 07:10:14.000000000 +0100
  8. Change: 2008-04-05 07:35:22.000000000 +0100

转载自:atime, ctime and mtime in Unix filesystems - Unix Tutorial

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值