Skip to content

Instantly share code, notes, and snippets.

@QNimbus
Last active May 27, 2025 14:04
Show Gist options
  • Save QNimbus/17fcdf7c6de5d9f9d2b4a23f3deed1ac to your computer and use it in GitHub Desktop.
Save QNimbus/17fcdf7c6de5d9f9d2b4a23f3deed1ac to your computer and use it in GitHub Desktop.
Bash aliases #shell #bash #linux
# Safe replacement for 'rm'
function rm() {
# Check if trash-put is available
if ! command -v trash-put >/dev/null 2>&1; then
echo -e "โŒ 'trash-put' not found! Nothing was deleted."
echo -e "๐Ÿ’ก You can install it with:\n\n sudo apt-get update && sudo apt-get install trash-cli\n"
return 1
fi
local args=()
local skipped_opts=()
local missing=()
# Separate options from actual file/folder arguments
for arg in "$@"; do
if [[ "$arg" == -* ]]; then
skipped_opts+=("$arg")
elif [[ -e "$arg" ]]; then
args+=("$arg")
else
missing+=("$arg")
fi
done
if [[ ${#args[@]} -eq 0 ]]; then
echo -e "โš ๏ธ 'rm' was called but no valid files or directories were found to delete."
if [[ ${#missing[@]} -gt 0 ]]; then
echo -e "โŒ Skipped missing/nonexistent: ${missing[*]}"
fi
return 1
fi
echo -e "โš ๏ธ 'rm' has been overridden! Using 'trash-put' instead to safely delete:โžœ ${args[*]}"
echo
# Show available safe deletion aliases
echo -e "๐Ÿ’ก Available safe deletion aliases:"
echo -e " ๐Ÿ—‘๏ธ del โ†’ trash-put"
echo -e " ๐Ÿ—‚๏ธ rmdir โ†’ trash-put"
echo -e " โ™ป๏ธ undo / undel โ†’ trash-restore"
echo -e " ๐Ÿ“‹ trash โ†’ trash-list"
echo -e " ๐Ÿšฎ emptytrash โ†’ trash-empty"
echo -e " ๐Ÿ”ฅ realrm โ†’ use real '/bin/rm' (โš ๏ธ irreversible!)"
echo
echo -e "๐Ÿ“ฆ Executing: trash-put ${args[*]}"
echo
trash-put "${args[@]}"
}
# Optional aliases for convenience
alias del='trash-put'
alias rmdir='trash-put'
alias emptytrash='trash-empty'
alias undo='trash-restore'
alias undel='trash-restore'
alias trash='trash-list'
alias realrm='/bin/rm'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment