1

For some reason, my finger memory has converted mv to rm. Until I manage to retrain those fingers, how might I have rm prompt when provided multiple locations (one of which likely does not exist)?

I know that the -I option will prompt on more than three files, and the -i will prompt on every file, but I need something similar that will prompt on more than one file.

terdon
  • 242,166
dotancohen
  • 15,864

2 Answers2

3

define function rm in your bash and check number of args. If there is only 1 arg, them run command rm to call the original command. Otherwise print a warning.

Something like:

function rm()
{
    if (( $# > 1 )); then
        read -r -p "sure? [y/n] " response
        case $response in
            [yY])
                command rm "$@"
                ;;
            *)
                echo "ignored"
                ;;
        esac
    else
        command rm "$@"
    fi
}

One thing to improve in my function is first to scan all parameters and if a parameter starts with - then consider the parameter as an option. So that function rm handles input like rm -f 1.txt.

0

Do frequent backups, use snapshots (à la time machine) if your file system is ZFS or btrfs, train your fingers to use the right commands, but I would advise to avoid changing rm behavior and semantics.

Doing it will sooner or later backfire, just like the obnoxious alias rm="rm -i"...

What I would advise instead in your case would be, when running interactive shells, to have rm disabled and being substituted by a custom rm that will do its original job. e.g. append these lines to your .bashrc

rm() {
    echo "rm disabled, use myRM instead"
}
alias myRM="command rm"

The annoyance created by the new rm function will be more efficient to help you getting rid of the typing mistake and you likely quickly cease to type rm.

While this advice looks counterproductive, the reason why I suggest doing it this way is there is no risk for you to be accustomed to a non standard still working behavior for such a dangerous Unix command. Sooner or later, you'll be logged under an account and/or another system without the hack in place and you'll remove files with no security prompt to save them.

On the other hand, if you get used to type myRM to remove files, doing it on foreign systems will be harmless.

Finally, should you nevertheless want to overload the rm command, here is a concise way to do it:

rm() {
  echo command rm $([ ${#} -gt 1 ] && echo \-i) "$@"
}
jlliagre
  • 61,204
  • Thank you. Actually, frequent backups won't help when I'm trying to move a few files that I just created (which is the case). For some reason I see that I've made the same mistake twice, so I'm trying to make making the mistake harder. – dotancohen Mar 14 '16 at 13:52
  • This does not answer the question. It does not solve the problem nor provide a workaround. “Change your habits” is explicitly rules out in the question. – Gilles 'SO- stop being evil' Mar 14 '16 at 22:59
  • @ dotancohen, yes, backups won't help for recently created files. Snapshots might save some of them though. ZFS snapshots are very lightweight so you can make one snapshot every minute and keep them for say, a couple of hours. Answer updated anyway with more explanations and a suggestion. – jlliagre Mar 14 '16 at 23:57
  • You are 100% correct about working on other systems. In fact, I do often work on other systems and adding a mental speedbump before the rm command sounds like a great idea. I even added a sleep 1 line for it to sink in. – dotancohen Mar 15 '16 at 07:04