Skip to content

Instantly share code, notes, and snippets.

@kianenigma
Created September 16, 2025 10:39
Show Gist options
  • Select an option

  • Save kianenigma/992e928cad6cd531880e50fc9371d7f2 to your computer and use it in GitHub Desktop.

Select an option

Save kianenigma/992e928cad6cd531880e50fc9371d7f2 to your computer and use it in GitHub Desktop.
wifi-cleanup.sh
#!/bin/bash
# WiFi Network Cleanup Tool
# This script helps you review and remove unwanted WiFi networks
# Colors for better readability
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check if running with sufficient privileges
if [[ $EUID -eq 0 ]]; then
echo "This script should not be run as root for safety reasons."
echo "You'll be prompted for your password when needed."
exit 1
fi
# Create backup
echo -e "${BLUE}Creating backup of current WiFi networks...${NC}"
backup_file="$HOME/Desktop/wifi_backup_$(date +%Y%m%d_%H%M%S).txt"
networksetup -listpreferredwirelessnetworks en0 > "$backup_file"
echo -e "${GREEN}Backup saved to: $backup_file${NC}\n"
# Get all networks into an array (reverse order to start with oldest)
IFS=$'\n' read -d '' -r -a networks < <(networksetup -listpreferredwirelessnetworks en0 | grep -v "Preferred networks on en0:" | sed 's/^[[:space:]]*//' | tac)
# Count networks
total=${#networks[@]}
removed=0
kept=0
echo -e "${YELLOW}Found $total WiFi networks to review${NC}\n"
echo "For each network, press:"
echo " [y/Y] or [Enter] = Keep"
echo " [n/N] = Remove"
echo " [q/Q] = Quit"
echo " [s/S] = Skip remaining and keep all"
echo -e "\nStarting review...\n"
# Process each network
for i in "${!networks[@]}"; do
network="${networks[$i]}"
# Skip empty lines
if [[ -z "$network" ]]; then
continue
fi
# Progress indicator
progress=$((i + 1))
echo -e "${BLUE}[$progress/$total]${NC} Network: ${YELLOW}$network${NC}"
# Ask user
read -p "Keep this network? [Y/n/q/s]: " -n 1 -r
echo # Move to next line
case "$REPLY" in
n|N)
echo -e "${RED}Removing: $network${NC}"
networksetup -removepreferredwirelessnetwork en0 "$network" 2>/dev/null
if [ $? -eq 0 ]; then
((removed++))
else
echo -e "${RED}Failed to remove: $network${NC}"
fi
;;
q|Q)
echo -e "\n${YELLOW}Quitting...${NC}"
break
;;
s|S)
echo -e "\n${YELLOW}Skipping remaining networks...${NC}"
((kept += total - progress + 1))
break
;;
*)
echo -e "${GREEN}Keeping: $network${NC}"
((kept++))
;;
esac
echo # Empty line for readability
done
# Final summary
echo -e "\n${BLUE}=== Summary ===${NC}"
echo -e "${GREEN}Networks kept: $kept${NC}"
echo -e "${RED}Networks removed: $removed${NC}"
# Show remaining count
remaining=$(networksetup -listpreferredwirelessnetworks en0 | grep -c -v "Preferred networks on en0:")
echo -e "${YELLOW}Total networks remaining: $remaining${NC}"
echo -e "\n${BLUE}Backup of original list saved to:${NC}"
echo "$backup_file"
# Optional: Show current network
current=$(airport -I | grep ' SSID' | awk '{print $2}')
if [[ ! -z "$current" ]]; then
echo -e "\n${GREEN}Currently connected to: $current${NC}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment