When modifying files programmatically (e.g., with `sed`), the file's modification timestamp is updated automatically. This can be problematic when the modification date carries semantic meaning, such as in [[Obsidian]] vaults where notes are sorted by modification time.
A possible approach, in [[macOS]]:
1. Save the original modification time using `stat`
2. Modify the file
3. Restore the original modification time using `touch`
```bash
# Save original modification time (macOS format)
mod_time=$(stat -f "%Sm" -t "%Y%m%d%H%M.%S" "$file")
# Modify the file (example: replace text with sed)
sed -i '' 's/old_text/new_text/' "$file"
# Restore the original modification time
touch -t "$mod_time" "$file"
```
A full example is:
```bash
#!/usr/bin/env bash
for file in *.md; do
# Save original modification time
mod_time=$(stat -f "%Sm" -t "%Y%m%d%H%M.%S" "$file")
# Modify the file
sed -i '' 's/old_pattern/new_pattern/' "$file"
# Restore the original modification time
touch -t "$mod_time" "$file"
echo "Modified: $file"
done
```