Skip to content

Instantly share code, notes, and snippets.

@vpetersson vpetersson/clean-pkg.sh
Last active Jun 14, 2019

Embed
What would you like to do?
Helper script for cleaning up pkg files
#!/bin/bash
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- sh-basic-offset: 4 -*-
set -euo pipefail
IFS=$'\n\t'
if [[ -z ${1} ]]; then
echo "Package name must be used as the argument."
exit 1
fi
PACKAGE=${1}
if [[ -z $(pkgutil --pkgs | grep ${PACKAGE}) ]]; then
echo "Package ${PACKAGE} does not exist."
exit 1
fi
ROOT=$(pkgutil --pkg-info ${PACKAGE} | grep volume | sed 's/volume: //g')
APP_PATH=$(pkgutil --pkg-info ${PACKAGE} | grep location | sed 's/location: //g')
GET_FILES=$(pkgutil --only-files --files ${PACKAGE})
GET_DIRS=$(pkgutil --only-dirs --files ${PACKAGE})
echo "Listing files and directories to be deleted:"
for f in ${GET_FILES}; do
echo ${ROOT}${APP_PATH}/${f}
done
for f in ${GET_DIRS}; do
echo ${ROOT}${APP_PATH}/${f}
done
echo -e "\n\nWARNING!\nLook throught the list carefully!\nThis could cause significant harm to your system.\n\n"
read -p "Delete files and directories? (y/N) " rm_files
if [[ ${rm_files} != 'y' ]]; then
echo "Aborting"
exit 1
fi
for f in ${GET_FILES}; do
rm -fv "${ROOT}${APP_PATH}/${f}"
done
for f in ${GET_DIRS}; do
rm -rfv "${ROOT}${APP_PATH}/${f}"
done
read -p "Forget package? (y/N) " forget_pkg
if [[ ${forget_pkg} == 'y' ]]; then
pkgutil --forget ${PACKAGE}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.