mtime vs ctime vs atime
While you are dealing with files in linux, you might wonder what are the differences between mtime
(modify time), ctime
(change time) and atime
(access time) .
Linux file timestamp
$ stat file1
File: file1
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 5123731 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1000/ j) Gid: ( 1000/ j)
Access: 2021-10-10 20:28:48.205548644 -0400
Modify: 2021-10-10 21:00:18.286812948 -0400
Change: 2021-10-10 21:14:46.659203297 -0400
Birth: -
We can use command stat filename
to check the timestamp of particular file.
atime
atime
stands for access time
, which means the last time when you access the content of the file, which may includes:
cat
a filegrep
a filefile
a filegvim
a file (without modification)
mtime
mtime
stands for modification time
, is when the file was last modified. When you change the contents of a file, the mtime
is changed.
ctime
ctime
stands for change time
. ctime
is changed when you change the contents of a file OR the metadata of the file.
- contents been changed
- ownership been changed (by command
chown
) - permission been changed (by command
chmod
) - file location been changed
- number of hard links
- file name been changed
Some tests
Now let’s do some tests to get familiar with timestamp.
j@ubuntu2004:~/tmp/1010$ stat file1
File: file1
Size: 7 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 5123731 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1000/ j) Gid: ( 1000/ j)
Access: 2021-10-10 21:21:54.024064972 -0400
Modify: 2021-10-10 21:26:56.327597042 -0400
Change: 2021-10-10 21:26:56.327597042 -0400
Birth: -
access time
j@ubuntu2004:~/tmp/1010$ grep a file1
ab
j@ubuntu2004:~/tmp/1010$ stat file1
File: file1
Size: 7 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 5123731 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1000/ j) Gid: ( 1000/ j)
Access: 2021-10-10 21:35:42.458118100 -0400
Modify: 2021-10-10 21:26:56.327597042 -0400
Change: 2021-10-10 21:26:56.327597042 -0400
Birth: -
j@ubuntu2004:~/tmp/1010$
We can see grep
changes the atime
but not mtime
and ctime
mtime
j@ubuntu2004:~/tmp/1010$ echo "test" > file1
j@ubuntu2004:~/tmp/1010$ stat file1
File: file1
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 5123731 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1000/ j) Gid: ( 1000/ j)
Access: 2021-10-10 21:35:42.458118100 -0400
Modify: 2021-10-10 21:37:01.875127281 -0400
Change: 2021-10-10 21:37:01.875127281 -0400
Birth: -
j@ubuntu2004:~/tmp/1010$
Changing the contents updates both mtime
AND ctime
ctime
j@ubuntu2004:~/tmp/1010$ chmod 777 file1
j@ubuntu2004:~/tmp/1010$ stat file1
File: file1
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 5123731 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1000/ j) Gid: ( 1000/ j)
Access: 2021-10-10 21:35:42.458118100 -0400
Modify: 2021-10-10 21:37:01.875127281 -0400
Change: 2021-10-10 21:38:09.539981686 -0400
Birth: -
j@ubuntu2004:~/tmp/1010$
Updating metadata changed ctime
but not mtime