Skip to content

Instantly share code, notes, and snippets.

@nanotaboada
Created May 8, 2025 12:45
Show Gist options
  • Save nanotaboada/a60dc532e431f0033f461de77910b0c3 to your computer and use it in GitHub Desktop.
Save nanotaboada/a60dc532e431f0033f461de77910b0c3 to your computer and use it in GitHub Desktop.
πŸ” Finds and safely deletes app leftovers in common macOS Library folders
#!/bin/bash
# ------------------------------------------------------------------------------
# πŸ” Finds and safely deletes app leftovers in common macOS Library folders
#
# This script searches macOS user and system Library directories for files or
# folders that match a given keyword (e.g., "Lorem", "Lorem Ipsum").
# It helps identify residual files from previously installed apps that may
# continue running in the background or occupy disk space.
#
# It supports interactive deletion with confirmation prompts for each match,
# and handles both files and directories.
#
# Features:
# - Searches safe-to-modify paths: ~/Library, /Library/LaunchAgents, etc.
# - Excludes SIP-protected paths like /System/Library
# - Handles directories using `rm -rv`
# - Automatically escalates privileges (sudo) for /Library paths
# - Pretty colored output and emojis for clarity
#
# Usage:
# ./find-in-library.sh "Keyword"
# ------------------------------------------------------------------------------
# Color setup
GREEN="\033[0;32m"
RED="\033[0;31m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
NC="\033[0m" # No Color
# Check for search term
if [ -z "$1" ]; then
echo -e "${RED}❌ Please provide a search term.${NC}"
echo -e "πŸ” Usage: ${YELLOW}$0 \"Your Keyword\"${NC}"
exit 1
fi
KEYWORD="$1"
echo -e "${BLUE}πŸ” Searching for: ${YELLOW}${KEYWORD}${NC}"
# Search paths (excluding SIP-protected ones)
SEARCH_DIRS=(
"$HOME/Library"
"$HOME/Library/LaunchAgents"
"/Library/LaunchAgents"
"/Library/LaunchDaemons"
)
FOUND=false
for DIR in "${SEARCH_DIRS[@]}"; do
if [ -d "$DIR" ]; then
echo -e "${BLUE}πŸ“ Searching in: ${DIR}${NC}"
RESULTS=$(find "$DIR" -iname "*$KEYWORD*" 2>/dev/null)
if [ -n "$RESULTS" ]; then
echo -e "${GREEN}βœ… Matches found:${NC}"
echo "$RESULTS"
IFS=$'\n'
for FILE in $RESULTS; do
echo -en "${YELLOW}πŸ—‘οΈ Delete?${NC} ${FILE} [y/N]: "
read -r CONFIRM
if [[ "$CONFIRM" =~ ^[Yy]$ ]]; then
if [ -d "$FILE" ]; then
echo -e "${BLUE}πŸ“‚ Removing directory...${NC}"
rm -rv "$FILE"
else
if [[ "$FILE" == /Library/* ]]; then
sudo rm -v "$FILE"
else
rm -v "$FILE"
fi
fi
else
echo -e "${BLUE}➑️ Skipped.${NC}"
fi
done
FOUND=true
else
echo -e "${YELLOW}⚠️ No matches found in this location.${NC}"
fi
fi
done
if [ "$FOUND" = false ]; then
echo -e "${RED}🚫 No matches found anywhere for '${KEYWORD}'.${NC}"
else
echo -e "${GREEN}πŸŽ‰ Done! All matching files processed.${NC}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment