Skip to content

Instantly share code, notes, and snippets.

@banyudu
Created February 5, 2026 06:48
Show Gist options
  • Select an option

  • Save banyudu/7e2e7db6560a0456878d93efb8355102 to your computer and use it in GitHub Desktop.

Select an option

Save banyudu/7e2e7db6560a0456878d93efb8355102 to your computer and use it in GitHub Desktop.
Safe rm wrapper for macOS - delegates to trash with rm-style flags
# Safe rm - move to trash instead of permanent delete
# Wraps macOS `trash` command, accepting standard rm flags
rm() {
local args=()
local verbose=false
local force=false
local interactive=false
while [[ $# -gt 0 ]]; do
case "$1" in
--)
shift
args+=("$@")
break
;;
--force) force=true ;;
--recursive) ;; # trash handles dirs natively
--verbose) verbose=true ;;
--interactive) interactive=true ;;
--dir) ;; # trash handles dirs natively
-*)
local flags="${1#-}"
for (( i=0; i<${#flags}; i++ )); do
case "${flags:$i:1}" in
r|R|d) ;; # trash handles dirs natively
f) force=true ;;
i|I) interactive=true ;;
v) verbose=true ;;
P|W) ;; # secure delete / undelete, N/A for trash
*) ;; # ignore unknown
esac
done
;;
*)
args+=("$1")
;;
esac
shift
done
if [[ ${#args[@]} -eq 0 ]]; then
echo "usage: rm [-f | -i] [-dPRrvW] file ..." >&2
return 64
fi
local trash_args=()
$verbose && trash_args+=("-v")
if $interactive && ! $force; then
for f in "${args[@]}"; do
read -q "REPLY?remove '$f'? " || { echo; continue; }
echo
command trash "${trash_args[@]}" "$f"
done
else
command trash "${trash_args[@]}" "${args[@]}"
fi
}
# Real rm when you actually need permanent deletion
alias rrm='/bin/rm'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment