Skip to content

Instantly share code, notes, and snippets.

@darwin
Last active April 2, 2020 13:54
Show Gist options
  • Save darwin/f346a5c5d0e3d58d7129e46d62261732 to your computer and use it in GitHub Desktop.
Save darwin/f346a5c5d0e3d58d7129e46d62261732 to your computer and use it in GitHub Desktop.
This scripts finds all files in given directory tree, reads their com.apple.metadata:kMDItemDownloadedDate xattr and changes last modification date to that value if exists.
#!/usr/bin/env bash
set -e
set_date_attr() {
local hex
hex=$(xattr -px "com.apple.metadata:kMDItemDownloadedDate" "$1")
if [[ $? = 0 ]]; then
local date_xml
date_xml=$(xxd -r -p <<< "$hex" | plutil -extract 0 xml1 -o - - | sed '4q;d' | sed -e 's/<[^>]*>//g')
echo -n "file '$1' has kMDItemDownloadedDate $date_xml"
local date_touch
date_touch=$(date -jf "%Y-%m-%dT%H:%M:%SZ" "$date_xml" "+%Y%m%d%H%M.%S")
echo " > touch -t \"$date_touch\" \"$1\""
touch -t "$date_touch" "$1"
else
echo "file '$1' does not have kMDItemDownloadedDate xattr"
fi
}
export -f set_date_attr
cd "$1"
find . -type f | xargs -I {} bash -c 'set_date_attr "{}"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment