Skip to content

Instantly share code, notes, and snippets.

@aaronk6
Last active April 19, 2025 20:47
Show Gist options
  • Save aaronk6/0f87380194ab199cf04a6465e72c6f0d to your computer and use it in GitHub Desktop.
Save aaronk6/0f87380194ab199cf04a6465e72c6f0d to your computer and use it in GitHub Desktop.
Rewrites files to remove stubborn extended attributes like com.apple.macl. Preserves creation and modification dates.
# This script rewrites a list of files to remove extended attributes like
# com.apple.macl, which can cause Time Machine backups to fail.
# It preserves creation and modification dates using SetFile/GetFileInfo.
#
# Provide the list of files via stdin, e.g.:
# echo "/path/to/file" | ./rewrite_file_clean.sh
# or
# cat filelist.txt | ./rewrite_file_clean.sh
#
# Requires: Xcode Command Line Tools (for SetFile and GetFileInfo)
# USE AT YOUR OWN RISK! The script overwrites the original files.
#!/bin/bash
set -eu -o pipefail
# Check dependencies
for tool in GetFileInfo SetFile; do
if ! command -v "$tool" >/dev/null 2>&1; then
echo "❌ Required tool '$tool' is missing. Install Xcode Command Line Tools with:"
echo " xcode-select --install"
exit 1
fi
done
# Check if input is coming from a pipe or redirection
if [ -t 0 ]; then
echo "Usage: echo \"/path/to/file\" | $0"
echo " or: cat filelist.txt | $0"
exit 1
fi
# Process input
while IFS= read -r file; do
[[ -z "$file" || ! -f "$file" ]] && continue
echo "→ Rewriting: $file"
# Timestamps
mod_time=$(stat -f "%Sm" -t "%Y%m%d%H%M.%S" "$file")
create_time=$(GetFileInfo -d "$file" 2>/dev/null || true)
tmpfile="${file}.tmp.$$"
cp -X -- "$file" "$tmpfile"
mv -- "$tmpfile" "$file"
touch -m -t "$mod_time" "$file"
[[ -n "$create_time" ]] && SetFile -d "$create_time" "$file" 2>/dev/null || true
echo "✔ Done: $file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment