Skip to content

Instantly share code, notes, and snippets.

@dmorel
Last active April 10, 2023 21:58
Show Gist options
  • Save dmorel/243fb0bbc2456e805922cfae7aa5f9eb to your computer and use it in GitHub Desktop.
Save dmorel/243fb0bbc2456e805922cfae7aa5f9eb to your computer and use it in GitHub Desktop.
#!/bin/bash
# Get the target folder and date from command-line arguments
TARGET_FOLDER="$1"
TARGET_DATE="$2"
# Check that both arguments were provided
if [ $# -ne 2 ]; then
echo "Usage: $0 folder date"
exit 1
fi
# Check that the target folder exists and is a subfolder of /Users/<username>
USERNAME=$(whoami)
if ! [[ "$TARGET_FOLDER" =~ ^/Users/"$USERNAME"/ ]]; then
echo "Error: $TARGET_FOLDER is not a subfolder of /Users/$USERNAME/"
exit 1
elif [ ! -d "$TARGET_FOLDER" ]; then
echo "Error: $TARGET_FOLDER does not exist or is not a directory"
exit 1
fi
# Check that the target date is a valid date in the past
if ! date -j -f "%Y-%m-%d %H:%M:%S" "$TARGET_DATE" >/dev/null 2>&1; then
echo "Error: $TARGET_DATE is not a valid date"
exit 1
elif [ "$(date -j -f "%Y-%m-%d %H:%M:%S" "$TARGET_DATE" +%s)" -ge "$(date +%s)" ]; then
echo "Error: $TARGET_DATE is not a date in the past"
exit 1
fi
# Ask for confirmation before proceeding
read -p "Are you sure you want to change the creation time of directories in $TARGET_FOLDER with creation date 01/24/1984 09:00:00 to $TARGET_DATE? [y/N] " confirm
if [[ ! "$confirm" =~ ^[yY](es)?$ ]]; then
echo "Aborted"
exit 1
fi
# Find all directories with the creation date of 01/24/1984 09:00:00 in the target folder
while IFS= read -r -d '' directory; do
# Use stat to get the creation date
creation_date=$(stat -f "%SB" -t "%Y-%m-%d %H:%M:%S" "$directory")
if [ "$creation_date" == "1984-01-24 09:00:00" ]; then
# Change the creation date to the target date
SetFile -d "$(date -j -f "%Y-%m-%d %H:%M:%S" "$TARGET_DATE" +%m/%d/%Y\ %H:%M:%S)" "$directory"
# Print out the directory name and old/new dates
echo "$directory: changed from $creation_date to $TARGET_DATE"
fi
done < <(find "$TARGET_FOLDER" -type d -print0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment