How can I edit a file without altering its modification dateĀ ? In command line. Is vi
able to thatĀ ?
MacOS – How to edit a file without altering its modif date
command linefilemacostext-editor
Related Question
- MacOS – How to have sshd re-read its config file (without killing ssh connections)
- MacOS – how to do a batch change (shift) of file creation date
- Command Line – How to Get the Expiration Date of a Certificate in a Keychain
- MacOS – Altering or touching a file does not always update the parent directory modification time
- MacOS – Edit text file in a folder owned by a different user on macOS? I have system admin password
- MacOS – Change file creation date to content created date using terminal
Best Answer
The only way I can think of is to store it's modification date before altering it and then to set it back again.
I'm not aware of any CLI tool that would allow you to do that, however years ago I've written something in C that I used to set the modification date on a file to arbitrary value. I know this is not the answer that you're looking for, but I would do this the following way:
stat()
system call (read more about it by issuingman 2 stat
).vi
or any other editor you likeutimes()
call (again, you can read more by issuingman 2 utimes
command). Please note,utimes()
sets both the access time and the modification time.Edit
Actually it seems like there's already a CLI tool that can do that. According to this answer: How to change the creation (st_birthtime) date/time of a file in Lion?, you can use
touch
command to change those times:touch -m
changes the modification time andtouch -a
changes the access time (you can use both options in one call). All you'd need to do then is use thestat
command line on the file before you modify it and display its access and modification times. Then do the edit and after that, usetouch
to set the access/modification times back to their values from before the edit.