Skip to content

Instantly share code, notes, and snippets.

@wteuber
Last active December 28, 2025 10:41
Show Gist options
  • Select an option

  • Save wteuber/ac114d6d8f5b7ed1ef74eb3f1dad6dd7 to your computer and use it in GitHub Desktop.

Select an option

Save wteuber/ac114d6d8f5b7ed1ef74eb3f1dad6dd7 to your computer and use it in GitHub Desktop.
Copy File dates on macOS
#!/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