basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Safety net for rm: -I instead of -i

    alias rm='rm -I' prompts once per command — when deleting more than three files or recursively — and shows how many items are about to go. rm -i asks about every file: forty logs mean forty prompts.

    How many times rm -i asks

    People usually add a safety net for rm with alias rm='rm -i' so rm asks before deleting. On a single file, that works fine. In a directory with forty logs, rm *.log asks forty times in a row, differing only in the filename:

    rm: remove regular file 'app-1.log'? y
    rm: remove regular file 'app-10.log'? y
    rm: remove regular file 'app-11.log'? y
    …

    With -r it’s worse: rm -i -r logs asks about every directory (descend into directory 'logs'?) and every file inside. To avoid hitting y forty times, you end up running rm -f or yes | rm — losing the prompt altogether, including when a glob matched more than you expected.

    What -I does

    -I prompts once per command and only for larger deletions: more than three files, or any recursive removal (-r). The prompt states how many items are matched:

    rm -I *.log
    # rm: remove 40 arguments?
    rm -I -r logs
    # rm: remove 1 argument recursively?

    If you expected three files and rm asks about forty, the glob caught too much — answer n. One to three files without -r are deleted without asking: -I won’t catch a typo in a single filename.

    Set it up

    # ~/.bash_aliases
    alias rm='rm -I'

    Reload the file with source ~/.bash_aliases (Bash aliases). The full everyday set is in Everyday aliases.

    The alias applies only where your file is loaded. In scripts, on another server, or with \rm, there is no prompt.

    If it doesn’t work

    rm -rf old/ deleted the directory without asking, even though ~/.bash_aliases sets rm -I

    Cause
    Among -f, -i, and -I, the last flag wins. The alias puts -I first, and your -f comes after it and cancels the prompt.
    Fix
    That’s intended: -f means “don’t ask”. The safety net applies to rm and rm -r without -f.

    rm nginx.conf deleted the file without asking

    Cause
    -I doesn’t prompt for one to three files without -r — that’s the trade-off for one prompt instead of forty.
    Fix
    For a file you can’t afford to lose, run rm -i nginx.conf: the explicit -i comes after -I from the alias and prompts.

    How to check

    type rm
    # rm is aliased to `rm -I'
    cd "$(mktemp -d)" && touch 1 2 3 4 && rm 1 2 3 4
    # rm: remove 4 arguments?

    Answer n — the files stay put.