Skip to content

Instantly share code, notes, and snippets.

@grahampugh
Last active October 8, 2020 15:19
Show Gist options
  • Save grahampugh/afc81b60ce07114d8a7f to your computer and use it in GitHub Desktop.
Save grahampugh/afc81b60ce07114d8a7f to your computer and use it in GitHub Desktop.
Search and delete Munki repo items
#!/bin/bash
### A script to search your Munki repo and offer to delete items. Use with care!
### It will search all directories including pkgs, pkgsinfo and icons.
### Syntax: /path/to/munkirm -d <search-term>
### For example, /path/to/munkirm -d xcode
### Search is case insensitive.
### Options are y or Y to delete, n, N or anything else to skip, and q or Q to quit
### If any changes are made, `makecatalogs` is run
### Put munkirm in /usr/local/munki/ if you wish to run from all directories
# Munki repo - change to match your path
MUNKI_REPO=`defaults read ~/Library/Preferences/com.googlecode.munki.munkiimport.plist repo_path`
# Introductions
echo
echo "----------------------------"
echo " MUNKI FILE REMOVAL TOOL"
echo "----------------------------"
echo "Usage: munkirm -d <string>"
echo "<string> is case-insensitive"
echo "part strings OK, e.g. goog"
echo
# First level
while getopts ":d:" opt; do
# Check to see if munkiimport is configured
if [ -z ${MUNKI_REPO} ]; then
echo "### munkiimport not configured. Run `munkiimport --configure`"
echo
exit 1
fi
# Check to see if the repository is mounted
if [ ! -d ${MUNKI_REPO} ]; then
echo "### Munki repository not mounted! Cannot continue"
echo
exit 1
fi
case $opt in
d)
# echo "-d was triggered, Parameter: $OPTARG" >&2
PKG="$OPTARG"
# write find results to temporary file
#find $MUNKI_REPO -type f -iname "*$PKG*" > /tmp/list.txt
find $MUNKI_REPO -type f -iname "*$PKG*" | awk -v FS=/ -v OFS=/ '{ print $NF,$0 }' | sort -n -t / | cut -f2- -d/ > /tmp/list.txt
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ -s /tmp/list.txt ]; then
# Print the results first
echo
echo "Found these files:"
echo
cat /tmp/list.txt
echo
# Now offer up each file for deletion
for file in `cat /tmp/list.txt`; do
read -p "Delete $file (y/n/q)? " -n 1 input
case $input in
y|Y ) echo
rm -r $file
echo "$file Deleted!"
echo
REMAKE=1
;;
q|Q ) echo
echo "skipped to end"
echo
break
;;
* ) echo
echo "skipped"
echo
;;
esac
done
else
# No match, therefore no file
echo "Files not found!"
echo
fi
rm /tmp/list.txt
# Update the repo if required
if [[ $REMAKE == 1 ]]; then
if hash makecatalogs 2>/dev/null; then
echo "### Catalogs have changed - running makecatalogs"
echo
/usr/local/munki/makecatalogs $MUNKI_REPO
else
echo "### Catalogs have changed but you don't have makecatalogs installed!"
echo "### Now run makecatalogs on a Mac with munkitools installed to effect the changes"
echo
fi
else
echo "### Catalogs have not changed. Exiting..."
echo
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment