#!/bin/bash
# Usage: ./find_problematic_filenames.sh [starting_directory]
# Finds names with control chars or common "bad" characters

DIR="${1:-.}"

echo "=== Problematic filenames (control chars or common bad chars) ==="

# Control characters (0x00-0x1F, 0x7F) + some common troublemakers
# Note: / cannot appear in a filename anyway
LC_ALL=C find "$DIR" -print0 2>/dev/null |
  grep -a -z $'[\x00-\x1F\x7F\n\r\t:*\?"<>|\\\\]' |
  while IFS= read -r -d '' file; do
    # Highlight the bad characters in output (optional)
    echo -n "Found problematic: "
    echo "$file" | LC_ALL=C sed 's/[\x00-\x1F\x7F\n\r\t:*\?"<>|\\\\]/[\x1b[31m&\x1b[0m]/g'
  done

echo ""
echo "=== Additionally: names with leading/trailing spaces or dots ==="
LC_ALL=C find "$DIR" \( -name ' *' -o -name '* ' -o -name '.*' -o -name '*.' \) -print
