Edit a file without changing its timestamp on Linux

Usually anytime a file a edited, its timestamp is also changed. There are cases when you want to change the content of a file but preserve its timestamp. Here are quick steps to do it on Linux.

First create a tmp file using touch with same timestamp as main file:

$ touch -r file.txt file.txt.tmp

The option -r (or –reference) uses file’s time instead of current time. You can use stat to check the timestamps of both files.

$ stat file.txt file.txt.tmp

Now edit the main file and make desired changes. Then use the touch command to touch the main file with timestamp of tmp file.

$ touch -r file.txt.tmp file.txt

Note that it will not affect the “change time” (creation time) of the file as it gets created when inode for the file is created.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments