Created
February 5, 2026 06:48
-
-
Save banyudu/7e2e7db6560a0456878d93efb8355102 to your computer and use it in GitHub Desktop.
Safe rm wrapper for macOS - delegates to trash with rm-style flags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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