Skip to content

Instantly share code, notes, and snippets.

@brunerd
Last active June 21, 2025 17:16
Show Gist options
  • Save brunerd/d9ea487b7d2faab9712a221592a2ee58 to your computer and use it in GitHub Desktop.
Save brunerd/d9ea487b7d2faab9712a221592a2ee58 to your computer and use it in GitHub Desktop.
A hacky example to clean the com.apple.macl attribute from a file using zip to sidestep SIP on Catalina
#!/bin/bash
#clean the com.apple.macl attribute from a file or folders using zip to sidestep SIP on Catalina
#WARNING: This will overwrite the original file/folders with the zipped version - DO NOT use on production data
#hold down command key at launch or touch /tmp/debug to enable xtrace command expansion
commandKeyDown=$(/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSCommandKeyMask > 1')
[ "$commandKeyDown" = "True" -o -f /tmp/debug ] && set -x && xtraceFlag=1
#hacky example to clean the com.apple.macl attribute from a file using zip to sidestep SIP
: <<-EOL
MIT License
Copyright (c) 2020 Joel Bruner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOL
#############
# VARIABLES #
#############
target="${1}"
OSVersionString=$(sw_vers -productVersion)
majorVersion=$(cut -d. -f1 <<< $OSVersionString)
minorVersion=$(cut -d. -f2 <<< $OSVersionString)
#############
# FUNCTIONS #
#############
function maclZipClean
{
local target="${1}"
local randomeZipName="maclClean-${RANDOM}.zip"
#separate the filename from the folder
local fileName="$(basename "$target")"
local fileFolder="$(dirname "$target")"
#go into the folder - this method avoid the parent folders above
cd "$fileFolder"
#make a zip in the home folder (the only guaranteed folder we have access to) of the filename
zip -r ~/"$randomeZipName" "$fileName"
local exitCode=$?
#if something goes wrong bail
if [ "${exitCode}" -ne 0 ]; then
echo "Something went wrong zipping $fileName (${exitCode}), exiting."
exit
fi
#otherwise remove the target we zipped
rm -rf "$target"
#unzip the zip file with the cleaned file(s) inside, overwriting/updating
unzip -u ~/"$randomeZipName" -d "$fileFolder"
#remove the temp zip
rm ~/"$randomeZipName"
}
function maclCleanXattr
{
local target="${1}"
#perhaps you are running this script on 10.14 and under, why do it the hard way then?!
xattr -rd com.apple.macl "$target"
}
########
# MAIN #
########
#if we are 10.14 and below use xattr (Big Sur seems to let you but in some cases not, so err on caution)
if [ "${majorVersion}" -eq 10 ] && [ "${minorVersion}" -le 14 ]; then
maclCleanXattr "${target}"
#otherwise we'll do our nifty zip way
else
maclZipClean "${target}"
fi
@skull-squadron
Copy link

The above is/was double work. com.apple.macl cannot presently be deleted with sudo xattr and Terminal/iTerm in Dev Tools. There's no point to checking os versions because you got here because it didn't work.

#!/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"

Boom, works on 15.5 and back to probably 10.5 on PPC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment