Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does the touch command also change ctime?

# stat main.c
Access: 2023-12-14 23:43:08.299761676 +0800
Modify: 2023-12-14 23:43:08.274761678 +0800
Change: 2023-12-14 23:43:08.274761678 +0800
 Birth: -

# touch main.c
# stat main.c (all the three times changed.)
Access: 2023-12-14 23:43:37.956758479 +0800
Modify: 2023-12-14 23:43:37.893758486 +0800
Change: 2023-12-14 23:43:37.893758486 +0800
 Birth: -

# for -a option,atime and ctime will change.
# for -m option,atime mtime,ctime all three will change.

I am confused about why does the touch command also change ctime?

touch changes the access and/or modification timestamps of the specified files.
(As you can see,only atime and mtime are mentioned.)

# options:
-a. Change the access timestamp only.
-m. Change the modification timestamp only.

# https://www.gnu.org/software/coreutils/manual/html_node/touch-invocation.html

So I am confused.

And after the touch command,the content of main.c is the same as before.

But the mtime has changed.(I cannot understand.)

like image 581
hhx Avatar asked Dec 29 '25 21:12

hhx


1 Answers

A summary that I think is correct after lots of googling and reading existing answers to similar questions:

touch only has options to modify atime (access time) and mtime (modification time), both simultaneously by default, because they are the only 2 timestamps a user should modify and can set to any specific timestamp they like while ctime (change time) is intended to be the current system time when the file was changed (and so by design it's harder for a user to set to a specific time).

ctime (Change time) is not set when atime is set because neither the file's contents nor attributes (apart from atime) have been set and so nothing about the file has Changed.

ctime is modified when mtime is set because mtime is an attribute of the file's meta-data, and ctime is set when any of the file's meta-data is set, other than atime.

In addition to the above, though, unlike other commands that update the access timestamp such as cat, touch always updates ctime even if called with -a to only update the access time and even if no timestamps actually changed, note when the timestamps change below:

$ printf 'Hello World\n' > file

$ stat file | tail -4
Access: 2023-12-14 11:13:06.850045900 -0600
Modify: 2023-12-14 11:13:06.850045900 -0600
Change: 2023-12-14 11:13:06.850958200 -0600
 Birth: 2023-11-28 09:06:22.968048500 -0600

$ cat file
Hello World

$ stat file | tail -4
Access: 2023-12-14 11:13:21.743099800 -0600 <- changed
Modify: 2023-12-14 11:13:06.850045900 -0600
Change: 2023-12-14 11:13:06.850958200 -0600
 Birth: 2023-11-28 09:06:22.968048500 -0600

$ touch -a file

$ stat file | tail -4
Access: 2023-12-14 11:13:49.667423400 -0600 <- changed
Modify: 2023-12-14 11:13:06.850045900 -0600
Change: 2023-12-14 11:13:49.667271500 -0600 <- changed
 Birth: 2023-11-28 09:06:22.968048500 -0600

Regarding the OPs' comment:

I am also confused that touch doesnot modify the file's content,but mtime is changed.why?

because you told it to. touch is the command you call to update a files modification and/or access time, so that's what it does when you call it.

For more information see:

  • https://www.gnu.org/software/coreutils/manual/html_node/touch-invocation.html
  • why-does-touch-a-also-set-the-ctime
  • https://man7.org/linux/man-pages/man2/utimensat.2.html
  • Setting creation or change timestamps
  • https://smarttech101.com/relatime-atime-noatime-strictatime-lazytime/
like image 103
Ed Morton Avatar answered Dec 31 '25 12:12

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!