Skip to content

Instantly share code, notes, and snippets.

@daigotanaka
Last active March 6, 2016 19:10
Show Gist options
  • Save daigotanaka/807687e1c28262af10a3 to your computer and use it in GitHub Desktop.
Save daigotanaka/807687e1c28262af10a3 to your computer and use it in GitHub Desktop.
Uninstall things you installed with a pkg on Mac
#!/bin/bash
# Uninstall things you installed with a pkg on Mac
# Tested on El Capitan
# Please run this at your own risk
# Read: http://superuser.com/questions/36567/how-do-i-uninstall-any-apple-pkg-package-file
if [ "$EUID" -ne 0 ]
then
echo "Please run as root"
exit
fi
if [ "$#" -ne 1 ]; then
echo "Give me a pkg name. Hint: cat /Library/Receipts/InstallHistory.plist"
exit
fi
root=$(pkgutil --pkg-info $1 | grep location | cut -f 2 -d ' ')
if [[ $root = "" ]]
then
echo "Could not find package."
echo "Looking for the package name? Try cat /Library/Receipts/InstallHistory.plist"
exit
fi
echo "Package is insetalled at " $root
pushd /$root
echo "Files:"
pkgutil --only-files --files $1
echo
echo "Dirs:"
pkgutil --only-dirs --files $1
echo
read -r -p "Are you sure to uninstall? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
pkgutil --only-files --files $1 | tr '\n' '\0' | xargs -n 1 -0 sudo rm -if
pkgutil --only-dirs --files $1 | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir
pkgutil --forget $1
echo "Uninstalled " $1
else
echo "Package is not uninstalled."
fi
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment