As you probably know, the “access time” and “modify time” time stamps of the file will be changed to the current time after the file is edited or modified.
Sometimes you may need to keep old timestamps for some reason even after editing or modifying files.
This quick tutorial explains how to edit a file without changing its timestamps on Linux systems.
Editing a file without changing its timestamp on Linux
The file timestamp can be updated using the command touch…
Time stamps are also updated when we manually add content to or remove data from the file.
If you want to change the contents of files without changing its timestamps, there is no direct way to do this. But it is possible!
We can use one of the touch command options -r (link) to preserve the timestamps of the file after editing or modification. The -r option is used to set the timestamps of one file to the timestamps of another.
I have a text file named itsecforu.txt.
Let’s take a look at the timestamps of this file using the stat command:
$ stat itsecforu.txt File: itsecforu.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 mentioned, if we change the content or metadata of this file, the timestamps will also change.
$ touch -r itsecforu.txt itsecforu.timestamp
Let’s check the timestamps of the new file:
$ stat itsecforu.timestamp File: itsecforu.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: -
The atime and mtime of both files are the same!
Now make changes to the main file, that is itsecforu.txt.
As you might have guessed, the timestamps of the main file will change.
Finally, copy the timestamps of the new file, for example itsecforu.timestamp, into the main file:
$ touch -r itsecforu.timestamp itsecforu.txt
The main file now has old timestamps before it was edited / modified.
Create a new file with the same timestamps as the main file using the touch command, make changes to the main file, and set the new file’s timestamps to the main file with the -r option.
Sponsored by this article – https://youproxy.ru/