Skip to content

Instantly share code, notes, and snippets.

@HackingGate
Created April 21, 2025 12:49
Show Gist options
  • Save HackingGate/e7ab66198695f07a9fa2930c721bf0af to your computer and use it in GitHub Desktop.
Save HackingGate/e7ab66198695f07a9fa2930c721bf0af to your computer and use it in GitHub Desktop.
Recursively find and delete all Emacs backup files (ending in '~') in the given directory (defaults to current directory).
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------------------------------------------------------
# cleanup-emacs-backups.sh
#
# Recursively find and delete all Emacs backup files (ending in '~') in
# the given directory (defaults to current directory).
#
# Usage:
# ./cleanup-emacs-backups.sh # cleans up under $PWD
# ./cleanup-emacs-backups.sh /path # cleans up under /path
# -----------------------------------------------------------------------------
# Directory to scan (default to current)
TARGET_DIR="${1:-.}"
# Confirm the directory exists
if [[ ! -d "$TARGET_DIR" ]]; then
echo "Error: '$TARGET_DIR' is not a directory." >&2
exit 1
fi
echo "Scanning for Emacs backup files in: $TARGET_DIR"
# List what will be deleted
find "$TARGET_DIR" -type f -name '*~' -print
# Prompt for confirmation
read -rp "Delete all these files? [y/N] " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo "Deleting..."
find "$TARGET_DIR" -type f -name '*~' -delete
echo "All Emacs backup files removed."
else
echo "Aborted. No files were deleted."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment