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.
4 comments
Hi,
Thanks a lot…
Nice, but…
1. When/why to do that?
2. What about the change time?
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.