Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Last active March 28, 2017 21:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phrohdoh/4f25a749026fc64389b297426ddfbaeb to your computer and use it in GitHub Desktop.
Save phrohdoh/4f25a749026fc64389b297426ddfbaeb to your computer and use it in GitHub Desktop.
Bash script to uninstall macOS 'pkg's
#!/bin/bash
# I highly suggest you run this with `--dry-run` and read the output before
# you actually uninstall any packages with this script.
# Example usage:
# ./uninstall-pkg.bash powershell --verbose --dry-run
# LICENSE: MIT
# Copyright 2017 Taryn Hill <taryn@phrohdoh.com>
# 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.
PKGUTIL=/usr/sbin/pkgutil
log() {
printf "[uninstall-pkg] $1\n"
}
err() {
log "Abort: $1"
exit 1
}
[[ -z "$1" ]] && err "Must give a package id (see \`$PKGUTIL --packages\`)" || { PACKAGE_NAME="$1" ; shift ; }
IS_DRY_RUN=0
IS_VERBOSE=0
while test $# != 0; do
case "$1" in
--dry-run) IS_DRY_RUN=1 ;;
--verbose) IS_VERBOSE=1 ;;
esac
shift
done
PKG_INFO="$($PKGUTIL --pkg-info "$PACKAGE_NAME" 2>&1)"
[[ $PKG_INFO == "No receipt for '$PACKAGE_NAME' found at"* ]] && {
err "$PACKAGE_NAME is not installed"
}
PKG_MOUNT_LOCATION=$($PKGUTIL --pkg-info "$PACKAGE_NAME" | grep location | cut -d':' -f 2 | xargs)
[[ -z "$PKG_MOUNT_LOCATION" ]] && {
[[ "$IS_VERBOSE" -eq 1 ]] && log "PKG_MOUNT_LOCATION is empty, setting to \"/\""
PKG_MOUNT_LOCATION="/"
}
[[ "$IS_VERBOSE" -eq 1 ]] && log "cd $PKG_MOUNT_LOCATION"
cd "$PKG_MOUNT_LOCATION"
PKG_FILES=( $($PKGUTIL --only-files --files "$PACKAGE_NAME") )
for FILENAME in ${PKG_FILES[@]} ; do
FILENAME="${PKG_MOUNT_LOCATION}${FILENAME}"
need_sudo=0
case "$FILENAME" in
"/Library"* |\
"/Applications"*) need_sudo=1 ;;
*) ;;
esac
[[ "$need_sudo" -eq 1 ]] && err "You will need to run this script with sudo to uninstall '$PACKAGE_NAME'"
[[ "$IS_VERBOSE" -eq 1 ]] && log "rm $FILENAME"
[[ "$IS_DRY_RUN" -eq 0 ]] && rm "$FILENAME"
done
PKG_DIRS=( $($PKGUTIL --only-dirs --files "$PACKAGE_NAME" | tail -r) )
for DIRNAME in ${PKG_DIRS[@]} ; do
DIRNAME="${PKG_MOUNT_LOCATION}${DIRNAME}"
case "$DIRNAME" in
"/usr" |\
"/usr/local" |\
"/usr/local/bin" |\
"/usr/local/share" |\
"/usr/local/share/man" |\
"/usr/local/share/man/man1" |\
"/Applications" |\
"/Library" |\
"/Library/ScriptingAdditions") continue ;;
*) ;;
esac
need_sudo=0
case "$DIRNAME" in
"/Library"* |\
"/Applications"*) need_sudo=1 ;;
*) ;;
esac
[[ "$need_sudo" -eq 1 ]] && err "You will need to run this script with sudo to uninstall '$PACKAGE_NAME'"
[[ "$IS_VERBOSE" -eq 1 ]] && log "rmdir $DIRNAME"
[[ "$IS_DRY_RUN" -eq 0 ]] && rmdir "$DIRNAME" >/dev/null
last_exit=$?
[[ $last_exit -ne 0 ]] && {
log "Failed to \`rmdir $DIRNAME\` exit code $last_exit"
exit $last_exit
}
done
log "Manually remove the receipt with \`sudo $PKGUTIL --forget $PACKAGE_NAME\`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment