CLI: Backing up and restoring file modification times

📄 OneThingWell.dev wiki page | 🕑 Last updated: Sep 27, 2022

I recently needed to keep the exact file modification times as a part of a git repository. Since git doesn't support such functionality out of the box, I used a simple combination of find and touch commands as a part of the pre-commit hook.

Here are two versions of this approach, one with the stat command, and the other one with just the combination of find and touch.

1st version (stat)

find . -type f -exec stat -c 'touch -d "%y" "%n"' {} \; > restore_mtimes

Explanation:

The resulting restore_mtimes file looks something like this:

touch -d "2022-09-26 15:55:10.127898032 +0200" "./f2"
touch -d "2022-09-26 15:55:11.651909078 +0200" "./f3"
touch -d "2022-09-26 15:55:06.751873534 +0200" "./f1"

You can execute this file to restore the original modification times (either by making it executable with chmod +x restore_mtimes or sourcing it directly with . restore_mtimes).

2nd version (find & touch)

Since we only need the file modification time, we could optimize things by removing the unnecessary call to the stat command, and simply use the modification time provided by find command:

find . -type f -printf 'touch -d "%t" "%p"\n' > restore_mtimes

The time format returned by find is a bit different (but it should be also parsed correctly by touch):

touch -d "Mon Sep 26 15:55:10.1278980320 2022" "./f2"
touch -d "Mon Sep 26 15:55:11.6519090780 2022" "./f3"
touch -d "Mon Sep 26 15:55:06.7518735340 2022" "./f1"

Alternatives and additional things to consider:

VCS commit times

One alternative to backing up file modification times is reusing the last commit time from your versioning control system, e.g.: restoring file mtimes with git.

touch --no-create

In cases where you're planning to restore the modification times to a tree where some files may be deleted, it may be useful to add the --no-create flag (which will prevent touch from creating any new files).


Comments and suggestions

You can use this form to submit a suggestion or a comment.

If you find this site useful in any way, please consider supporting it.