Skip to content

Instantly share code, notes, and snippets.

@fernandofig
Last active March 6, 2016 21:12
Show Gist options
  • Save fernandofig/1f6d418d7e4698521b53 to your computer and use it in GitHub Desktop.
Save fernandofig/1f6d418d7e4698521b53 to your computer and use it in GitHub Desktop.
Quick and dirty script to remove .PKG installs on OSX
#!/bin/bash
SPECIALEXTLIST=(app action caction definition bundle kext prefPane plugin jdk fs framework)
function joinList { local IFS="$1"; shift; echo "$*"; }
function getListWithTerminators {
PROCLIST=()
for EXT in "${SPECIALEXTLIST[@]}"; do
PROCLIST[${#PROCLIST[@]}]="\.$EXT$"
if [ "$1" = "true" ]; then
PROCLIST[${#PROCLIST[@]}]="\.$EXT/"
fi
done
joinList "|" ${PROCLIST[@]}
}
function containsElement {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && echo 1 && return 0; done
echo 0
}
SPECIALEXTLISTEXPR_SIMPLE=$(getListWithTerminators)
SPECIALEXTLISTEXPR=$(getListWithTerminators true)
if [ "$USER" != "root" ]; then
echo "You should be root to run this script."
exit 1;
fi
if [ "$1" = "" ]; then
echo "Usage: $0 [-r] package-name"
echo " You can find the package name by executing pkgutil --pkgs"
echo " Without -r the files/directories that would be removed are only displayed."
exit 1;
fi
UNINST="0"
if [ "$2" != "" ]; then
PKGNAME=$2
if [ "$1" = "-r" ]; then
UNINST="1"
fi
else
PKGNAME=$1
fi
HASPKG=`pkgutil --pkgs | grep -c "$PKGNAME"`
if [ $HASPKG -eq 0 ]; then
echo "Specified package name does not seem to exist."
exit 1;
fi
if [ $HASPKG -gt 1 ]; then
echo "The specified package name seems to match more than one more installed package!"
exit 1;
fi
LOC=`pkgutil --pkg-info "$PKGNAME" | grep -i "Location" | sed 's/^location:[^a-zA-Z0-9]*//g'`
if [ "$LOC" = "(null)" ]; then
LOC=""
fi
LOC="/$LOC"
echo " * Package root: $LOC"
DEEPDIR=`basename "$LOC"`
DIREXT=`echo $DEEPDIR | sed 's/^.*\.//g'`
DIRISSPECIAL=0
if [ "$DIREXT" != "" ]; then
DIRISSPECIAL=$(containsElement "$DIREXT" "${SPECIALEXTLIST[@]}")
if [ $DIRISSPECIAL -eq 1 ]; then
echo -n " * Package location is special file"
if [ "$UNINST" = "0" ]; then
echo ", so that would be removed."
else
echo ". Removing that..."
rm -vr "$LOC"
pkgutil --force --forget "$PKGNAME"
fi
fi
fi
if [ $DIRISSPECIAL -eq 0 ]; then
APPFILES=`pkgutil --only-dirs --files "$PKGNAME" | egrep -i "$SPECIALEXTLISTEXPR_SIMPLE" | tail -r`
if [ "$UNINST" = "0" ]; then
echo " * Files to be removed ..."
pkgutil --only-files --files "$PKGNAME" | egrep -vi "$SPECIALEXTLISTEXPR"
if [ "$APPFILES" != "" ]; then
echo
echo " * App Files to be removed ..."
OIFS=$IFS
IFS=$'\n'
for ARQ in $APPFILES; do echo $ARQ; done
echo
echo " * Directories to be removed ..."
for PD in `pkgutil --only-dirs --files "$PKGNAME" | egrep -vi "$SPECIALEXTLISTEXPR" | tail -r`; do
echo $PD
done
IFS=$OIFS
else
echo
echo " * Directories to be removed ..."
pkgutil --only-dirs --files "$PKGNAME" | tail -r
fi
else
cd $LOC
echo " * Removing files ..."
pkgutil --only-files --files "$PKGNAME" | egrep -vi "$SPECIALEXTLISTEXPR" | tr "\n" "\0" | xargs -n 1 -0 rm -v
if [ "$APPFILES" != "" ]; then
echo
echo " * Removing App Files ..."
OIFS=$IFS
IFS=$'\n'
for ARQ in $APPFILES; do rm -vr $ARQ; done
echo
echo " * Directories to be removed ..."
for PD in `pkgutil --only-dirs --files "$PKGNAME" | egrep -vi "$SPECIALEXTLISTEXPR" | tail -r`; do
rm -f $PD/.DS_Store
rmdir $PD
done
IFS=$OIFS
else
echo
echo " * Directories to be removed ..."
pkgutil --only-dirs --files "$PKGNAME" | tail -r | tr "\n" "\0" | xargs -n 1 -0 sudo rmdir
fi
echo
echo " * Forgetting $PKGNAME ..."
pkgutil --force --forget "$PKGNAME"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment