Created
February 6, 2018 22:10
-
-
Save adriansr/94230f7b21b7e278a9c98c2ecc897077 to your computer and use it in GitHub Desktop.
Uninstall macOS pkg removing all files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
die() { | |
echo "error: $@" >&2 | |
exit 1 | |
} | |
test "$#" -gt 0 || die "Usage: $0 <package identifier>" | |
test "$(uname -s)" = "Darwin" || die "Must be run under macOS" | |
test $(id -u) -eq 0 || die "Must be root" | |
which -s pkgutil || die "pkgutil not available. Install XCode" | |
APP="$1" | |
pkgutil --pkg-info "$APP" > /dev/null || die "No found app by this package identifier" | |
for key in volume location | |
do | |
EXP="^$key: " | |
VAL=$(pkgutil --pkg-info "$APP" | grep "$EXP" | sed "s/$EXP//") | |
eval $key=\$VAL | |
done | |
BASE="$volume$location" | |
test -d "$BASE" || die "Resolved base directory '$BASE' doesn't exist" | |
pushd "$BASE" | |
pkgutil --only-files --files "$APP" | tr '\n' '\0' | xargs -0 -n 1 rm | |
pkgutil --only-dirs --files "$APP" | sort -r | tr '\n' '\0' | xargs -0 -n 1 rmdir | |
popd | |
pkgutil --forget "$APP" || die "Failed to remove the package from the database" | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment