Safer rm command

Prevent accidental deletion caused by rm with wildcards. This replaces your rm command so that it prevents certain deletions.

    # Prohibited: rm *
    # Prohibited: rm * foo.txt
    # Prohibited: rm foo.txt *
    # Allowed:    rm foo.txt
    # Allowed:    rm foo*.txt

    for arg in "${@}"; do
        if [[ "${arg}" == "*" ]]; then

    if $dangerous_wildcard_detected; then
        echo "cowardly refusing to run \`rm' with a dangerous wildcard"
        return 1

    # Turn on filename expansion (globbing).
    set +f

    for arg in "${@}"; do
        if [[ "${arg}" =~ " " ]]; then

    command rm "${args[@]}"
alias rm="set -f && safer_rm"


Leave a Reply