Skip to content

Instantly share code, notes, and snippets.

@nbodev
Created June 28, 2017 07:08
Show Gist options
  • Save nbodev/3efee1331e4e47df3cf843bec546b11a to your computer and use it in GitHub Desktop.
Save nbodev/3efee1331e4e47df3cf843bec546b11a to your computer and use it in GitHub Desktop.
uninstall pkg from mac os
#!/bin/bash
#date: 28/06/2017, author: nbodev
#remove package from mac os
# tested on mac os Sierra
#steps
#pkgutil --pkgs # list all installed packages
#pkgutil --files the-package-name.pkg # list installed files for a given package
#pkgutil --pkg-info the-package-name.pkg # check the location of a given package
#cd / # assuming the package is rooted at /...
#pkgutil --only-files --files the-package-name.pkg | tr '\n' '\0' | xargs -n 1 -0
#pkgutil --only-files --files the-package-name.pkg | tr '\n' '\0' | xargs -n 1 -0 sudo rm -f
#pkgutil --only-dirs --files the-package-name.pkg | tail -r | tr '\n' '\0' | xargs -n 1 -0
#pkgutil --only-dirs --files the-package-name.pkg | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir
#sudo pkgutil --forget the-package-name.pkg
#pkgutil --files the-package-name.pkg # list installed files
read -p "Enter package name: " name
pkgutil --pkgs | grep -i $name
read -p "Enter the package full name (e.g. the-package-name.pkg): " full_name
echo "************ running pkgutil --files $full_name"
pkgutil --files $full_name
if [ $? == 0 ]; then #package name correct
echo "************ running pkgutil --pkg-info $full_name"
pkgutil --pkg-info $full_name
read -p "Step 1/2 => Continue scanning the package, nothing will be deleted ? (Y)" answer
if [ $answer == "Y" ]; then
cd / # assuming the package is rooted at /...
echo "************ running pkgutil --only-files --files $full_name | tr '\n' '\0' | xargs -n 1 -0"
pkgutil --only-files --files $full_name | tr '\n' '\0' | xargs -n 1 -0
echo "************ running pkgutil --only-dirs --files $full_name | tail -r | tr '\n' '\0' | xargs -n 1 -0"
pkgutil --only-dirs --files $full_name | tail -r | tr '\n' '\0' | xargs -n 1 -0
else
echo "Aborting Step 1/2 ..."
exit 0
fi
read -p "Step 2/2 => Danger zone: delete the package ? (Y)" answer_danger
if [ $answer_danger == "Y" ]; then
cd / # assuming the package is rooted at /...
echo "************ running pkgutil --only-files --files $full_name | tr '\n' '\0' | xargs -n 1 -0 sudo rm -f"
pkgutil --only-files --files $full_name | tr '\n' '\0' | xargs -n 1 -0 sudo rm -f
echo "************ running pkgutil --only-dirs --files $full_name | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir"
pkgutil --only-dirs --files $full_name | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir
echo "************ running sudo pkgutil --forget $full_name"
sudo pkgutil --forget $full_name
echo "************ running pkgutil --files $full_name" #run again to check if the package is still here or not
pkgutil --files $full_name
else
echo "Aborting Step 2/2 ..."
exit 0
fi
else
echo "looks like the the package full name is wrong " $full_name
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment