As you may know, the "access time" and "modify time" timestamps of a file will be changed to the current time after the file is edited or modified. Sometimes, you might want to preserve the old timestamps for any reason even after editing or modifying the files. This brief guide explains how to edit a file without changing its timestamps in Linux.
If you don't know much about Linux file timestamps, refer the following guide.
Edit A File Without Changing Its Timestamps In Linux
A file's timestamps can be updated using touch
command. The timestamps also gets updated when we manually add contents in a file or remove data from it. If you want change the contents of files without changing its timestamps, there is no direct way to do it. But it is possible!
We can use one of the touch
command's option -r
(reference) to preserve file timestamps after editing or modifying it. The -r
option is used to set the timestamps of one file to the timestamp values of another.
I have a text file named ostechnix.txt
. Let us have look at the timestamps of this file with stat
command:
$ stat ostechnix.txt File: ostechnix.txt Size: 38 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 4351679 Links: 1 Access: (0775/-rwxrwxr-x) Uid: ( 1000/ sk) Gid: ( 1000/ sk) Access: 2020-11-12 19:47:55.992788870 +0530 Modify: 2020-11-12 19:47:55.992788870 +0530 Change: 2020-11-12 19:47:55.992788870 +0530 Birth: -
As stated already, if we change the contents or metadata of this file, the timestamps will also change.
Now create a new, empty file, for example ostechnix.timestamp
, with the same timestamps as ostechnix.txt
file using touch
command:
$ touch -r ostechnix.txt ostechnix.timestamp
Check the timestamps of the new file:
$ stat ostechnix.timestamp File: ostechnix.timestamp Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 4328645 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ sk) Gid: ( 1000/ sk) Access: 2020-11-12 19:47:55.992788870 +0530 Modify: 2020-11-12 19:47:55.992788870 +0530 Change: 2020-11-12 19:48:48.934235300 +0530 Birth: -
See? Both files' atime and mtime timestamps are same!
Now make the changes in the main file i.e. ostechnix.txt
. As you guessed, the main file's timestamps will change.
Finally, copy the timestamps of new file i.e. ostechnix.timestamp
to the main file:
$ touch -r ostechnix.timestamp ostechnix.txt
Now the main file has its old timestamps before it was edited/modified.
Please note that we can't preserve the change time (ctime) timestamps. It will always update to the current time.
TL;DR
Create a new file with same timestamps as main file with touch
command, do the changes in the main file and set the timestamps of the new file to main file with -r
option.
Hope this helps.
5 comments
Hi,
Thanks a lot…
Nice, but…
1. When/why to do that?
2. What about the change time?
I’m not the original author, but I can answer your questions.
Why do it? In my environment, I use blogging software that determines the order to show the entries based on the modification time of the file (i.e. newer files show first so blog entries appear in the order they were written). Occasionally, I want to go back to a previous entry and make a correction, fix a typo, or similar, but I don’t want that post to suddenly appear to be a new post (I want it to stay where it was in the display order). With this mechanism, I can edit the file but keep the modification time the same and the blog entry will stay in the right place relative to other entries.
What about the ctime? Well, you can’t do anything to keep the ctime from changing. The ctime refers to the inode “change” time. Any time you modify the timestamps, you change the inode, so the ctime gets updated. There is no system call interface that will let you modify the ctime. Now, that said, there IS a way to tweak the ctime on a file. It isn’t for the faint of heart, though. You have to use the file system debugger tools (fsdb and similar depending on the filesystem you are monkeying with). You’ll want to operate on an unmounted filesystem. These applications let you open the filesystem directly, browse around, find the particular inode you want to fiddle with, and edit it in place. Yeek. No, it’s not recommended, but it is doable.
What about mounting with NFS and using noatime?
If you mount a filesystem with noatime timestamp, the last accessed date will not be recorded. I haven’t personally tested “noatime” yet. But I think it increases speed because when a file is accessed (read from), it will also record that as being a time the file was accessed, and that writing to the file takes extra time, as does writing that extra data when it is being written to.