4

Sometimes I want to make a minor edit to a text file (e.g., fix a typo), but I want to keep its modification time unchanged. (Motivation: modtime is useful for listing directories in chronological order, for telling when a file's content was last substantively updated, etc. I don't want a trivial edit to obliterate this information.)

This is certainly possible: Unix allows me to manually set the modtime to anything I want (this does set the ctime to the present, which is fine by me). E.g., with touch -t <time>, or with the utimes(2) system call. This question is about a simple way to edit a file and then set the modification time to exactly what it was before editing. I am mainly interested in solutions that are editor-agnostic, i.e., if emacs has a save-file-no-update-mtime command, that's interesting but not what I'm looking for.

alexis
  • 5,759
  • Thanks! The linked question is tagged [vim], but the solutions are indeed editor-independent. IMHO the question hasn't been fully answered, though (touch -r is clever, but using a temporary file is clunky...) – alexis Apr 23 '16 at 12:14
  • 1
    yeah most answers there are editor-agnostic but Shâu Shắc's solution doesn't use a temporary file (it's gnu specific though) – don_crissti Apr 23 '16 at 12:23

1 Answers1

5

Here's a fairly simple solution, based on this answer to the related question discovered by @don_crissti. But I was hoping for something that doesn't use an extra file-- can anyone do better?

touch -r some-file.txt .timestamp 
emacs some-file.txt  # Or whatever
touch -r .timestamp some-file.txt 
rm .timestamp
alexis
  • 5,759