Technology

Remove files and directory (even the directory full of files).

MOHAMMED SADIK KPMOHAMMED SADIK KP
Remove files and directory (even the directory full of files).
//delete_all_files

#!/usr/bin/env bash
set -euo pipefail

# delete_all_files.sh - Safe deletion utility for a directory
# Usage:
#   ./delete_all_files.sh                # interactive, deletes top-level files only
#   ./delete_all_files.sh --all         # interactive, deletes everything (recursive)
#   ./delete_all_files.sh --yes --all   # non-interactive, perform deletion
#   ./delete_all_files.sh --dry-run --all  # show actions without deleting

PWD_DIR="$(pwd -P)"
if [ "${PWD_DIR}" = "/" ] || [ "${PWD_DIR}" = "${HOME}" ]; then
  echo "Refusing to run from ${PWD_DIR}. Run this in the target directory."
  exit 1
fi

DELETE_MODE="files" # files or all
DRY_RUN=0
AUTO_YES=0

while [ $# -gt 0 ]; do
  case "$1" in
    --all) DELETE_MODE="all"; shift ;;
    --files-only) DELETE_MODE="files"; shift ;;
    --dry-run) DRY_RUN=1; shift ;;
    --yes|-y) AUTO_YES=1; shift ;;
    --help|-h) echo "Usage: $0 [--all|--files-only] [--dry-run] [--yes]"; exit 0 ;;
    *) echo "Unknown option: $1"; echo "Use --help for usage"; exit 2 ;;
  esac
done

echo "Target directory: ${PWD_DIR}"
if [ "${DELETE_MODE}" = "all" ]; then
  echo "Mode: delete everything (recursive)"
else
  echo "Mode: delete top-level files only"
fi
if [ "${DRY_RUN}" -eq 1 ]; then
  echo "Dry run: no files will be removed"
fi

if [ "${AUTO_YES}" -eq 0 ]; then
  echo "Listing items that would be removed:"
  if [ "${DELETE_MODE}" = "all" ]; then
    find . -mindepth 1 -maxdepth 3 -print
  else
    find . -maxdepth 1 -type f -print
  fi
  printf "\n"
  read -p "Type 'yes' to proceed: " CONFIRM
  if [ "${CONFIRM}" != "yes" ]; then
    echo "Operation cancelled."
    exit 0
  fi
fi

if [ "${DRY_RUN}" -eq 1 ]; then
  echo "Dry-run complete. No changes made."
  exit 0
fi

# Perform deletion
if [ "${DELETE_MODE}" = "all" ]; then
  echo "Deleting everything under ${PWD_DIR} ..."
  find . -mindepth 1 -delete
else
  echo "Deleting top-level files in ${PWD_DIR} ..."
  find . -maxdepth 1 -type f -delete
fi

echo "Done."

Ensure the directory needs to be included in any filename like code-delete.sh and use it wisely.

Now I'll run the new script in dry-run mode to verify it lists items without deleting anything. I'll run it with bash ... --dry-run so it doesn't require executable permission.

/[Full-Path]/delete_all_files.sh --dry-run

How it works

  • Options: --all (recursive), --files-only (top-level files), --dry-run (show what would be removed), --yes (skip interactive confirmation).

  • Safety: refuses to run from / or your home directory; shows items before deletion.

  • Default behavior: interactive, deletes top-level files only.

Run it

  • Make executable (optional):

    chmod +x /[Full-Path]/delete_all_files.sh
  • Dry-run (safe test):

    bash /[Full-Path]/delete_all_files.sh --dry-run --all
  • To actually delete top-level files (interactive):

    bash /[Full-Path]/delete_all_files.sh
  • To actually delete everything without prompt:

    bash /[Full-Path]/delete_all_files.sh --all --yes

MOHAMMED SADIK KP

MOHAMMED SADIK KP

Software developer

Follow on X