31

How can I change the "change" date of a file? Using touch doesn't work:

$ touch -t 9901010000 test;stat test
  File: `test'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fe01h/65025d    Inode: 11279017    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    x)   Gid: ( 1000/    x)
Access: 1999-01-01 00:00:00.000000000 +0100
Modify: 1999-01-01 00:00:00.000000000 +0100
Change: 2012-04-08 19:26:56.061614473 +0200
 Birth: -
2
30

You cannot change the ctime by ordinary means. This is by design: the ctime is always updated to the current when you change any of the file's metadata, and there is no way to impose a different ctime. To change the ctime of a file, you need to do one of the following:

  • Set the system time to the ctime you want to impose, then touch the file, then reset the system time.
  • Modify the kernel to add an interface to change the ctime.
  • Access the disk image directly (e.g. with debugfs) and twiddle the bits on the disk (don't do it while the filesystem is mounted).
1
14

You have the answer on related SO question pointed by jw013, for extX, on unmounted disk :

# Update ctime
debugfs -w -R 'set_inode_field /tmp/foo ctime 201001010101' /dev/sda1

# Drop vm cache so ctime update is reflected
echo 2 > /proc/sys/vm/drop_caches
4

The ctime of a file is updated when any of the metadata is changed.

$ ls -l x.py
-rw-rw-r--. 1 ignacio ignacio 485 Mar 26  2010 x.py
$ stat -c %z x.py
2010-03-26 11:57:56.237068175 -0400
$ chown ignacio x.py
$ stat -c %z x.py
2012-04-08 15:31:33.682383575 -0400
$ ls -l x.py
-rw-rw-r--. 1 ignacio ignacio 485 Mar 26  2010 x.py
1
  • but how modify it without update it Apr 8 '12 at 20:11
3

$ NOW=$(date) && date -s "2030-08-15 21:30:11" && touch file.txt && date -s "$NOW"

https://www.shellhacks.com/fake-file-access-modify-change-timestamps-linux

1

evandrix's answer excerpted in the next line,
NOW=$(date) && date -s "2030-08-15 21:30:11" && touch file.txt && date -s "$NOW"
needs to be improved as described below :

In some systems like mine, date output doesn't give a valid format to set with date -s

My system bash shell version : GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
My system date command version : date (GNU coreutils) 8.30

My system date command output and setting the date with this format is:

# date
Tue 21 Jan 2020 01:36:22 PM +03
# date -s "Tue 21 Jan 2020 01:36:22 PM +03"
date: invalid date ‘Tue 21 Jan 2020 01:36:22 PM +03’

So it is necessary to improve evandrix answer;
It would be better to assign the NOW variable to the timestamp value
change NOW=$(date) to NOW=$(date +@%s)

  NOW=$(date +@%s) && date -s "2030-08-15 21:30:11" && \
  touch file.txt && date -s "$NOW"

Adding sudo command for non root user

  sudo bash -c 'NOW=$(date +@%s);
                date -s "2030-08-15 21:30:11";
                touch file.txt;
                date -s "$NOW"'

or

  sudo bash -c 'NOW=$(date +@%s); date -s "$2"; touch "$1"; date -s "$NOW"' -- \
                "file.txt" "2030-08-15 21:30:11"

In this way for easy use, the filename and setting date are assigned as arguments at the end of the line.

0

I use ln for this case. The meta data is changed to increase and decrease the reference counter.

# $file contains the filename
ln "$file" "$file.xxx.tmp"
rm -f "$file.xxx.tmp"

Here I use .xxx.tmp as extension for the temporary file and hope, that it will not used by any other.

1
  • Welcome to the site and thank you for your contribution. However, I don't think this is what the OP wants, because that could also be accomplished with the touch command shown. The problem seems to be that the OP wants to set the change time to an arbitrary value, as can be done with access and modification time.
    – AdminBee
    Sep 29 '20 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.