Skip to content

Instantly share code, notes, and snippets.

@skull-squadron
Created June 21, 2025 17:19
Show Gist options
  • Save skull-squadron/a9c2d03988ae9ac8ede1e13e5b3599e5 to your computer and use it in GitHub Desktop.
Save skull-squadron/a9c2d03988ae9ac8ede1e13e5b3599e5 to your computer and use it in GitHub Desktop.
remove com.apple.macl xattr
#!/bin/bash
# Free fucking beer license. It may break and you assume all responsibility.
set -eEuo pipefail
die() {
echo >&2 "$@"
exit 1
}
[[ $# = 1 ]] || die "$0 {target} <-- missing file or directory to remove all xattrs from"
target="$(/usr/bin/readlink -f "$1")"
[[ -f "$target" || -d "$target" ]] || die "$0 {target} <-- must be a file or a directory"
prefix="$(/usr/bin/dirname "$target")/.$(/usr/bin/basename "$target")"
new="$prefix.$$.new"
orig="$prefix.$$.orig"
lock="$prefix.macl-lock"
# Remove stuff on normal or abnormal termination
# $orig is not deleted here in case of error
trap 'e=$?; trap - EXIT; /bin/rm -rf "$new" "$lock"; exit $e' EXIT
# mkdir is atomic on macOS, and suffices when flock is unavailable
if ! /bin/mkdir "$lock"; then
echo >&2 "$0 ERROR"
echo >&2 " Operation already in progress OR"
echo >&2 " insufficient rights to write $lock"
die " Maybe \`sudo $0 $*\`?"
fi
# Prevent creating duplicate nested directories
if [[ -d "$target" ]]; then
rsync_target="$target/"
else
rsync_target="$target"
fi
# Hard copy $target as $new
/usr/bin/rsync -av "$rsync_target" "$new"
# Verify original == $new
/usr/bin/diff -qr "$target" "$new" || die "$0 copy failure (out of space OR bad media?). Original unchanged."
# Rotate original out of the way
/bin/mv -v "$target" "$orig"
# Slide in the new one
/bin/mv -v "$new" "$target"
# Remove the now duplicate original
/bin/rm -rf "$orig"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment