Last active
December 28, 2025 10:41
-
-
Save wteuber/ac114d6d8f5b7ed1ef74eb3f1dad6dd7 to your computer and use it in GitHub Desktop.
Copy File dates on macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env zsh | |
| # Usage: copy_dates.sh source_file dest_file | |
| # Requires: Xcode Command Line Tools (for SetFile) | |
| set -euo pipefail | |
| if (( $# != 2 )); then | |
| echo "Usage: $0 source_file dest_file" >&2 | |
| exit 1 | |
| fi | |
| src=$1 | |
| dst=$2 | |
| if [[ ! -e $src ]]; then | |
| echo "Source does not exist: $src" >&2 | |
| exit 1 | |
| fi | |
| if [[ ! -e $dst ]]; then | |
| echo "Destination does not exist: $dst" >&2 | |
| exit 1 | |
| fi | |
| # 1) Copy access & modification times (simple and reliable) | |
| touch -r "$src" "$dst" | |
| # 2) Get source creation time (seconds since epoch) | |
| c_epoch=$(stat -f '%B' "$src") | |
| # 3) Format for SetFile: MM/DD/YYYY HH:MM:SS | |
| c_str=$(date -r "$c_epoch" '+%m/%d/%Y %H:%M:%S') | |
| # 4) Apply creation time to destination | |
| SetFile -d "$c_str" "$dst" | |
| # Optional: also force modification time to match exactly | |
| m_epoch=$(stat -f '%m' "$src") | |
| m_str=$(date -r "$m_epoch" '+%m/%d/%Y %H:%M:%S') | |
| SetFile -m "$m_str" "$dst" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment