Score:4

How to make confirmation when deleting files via terminal

mx flag
cd Desktop/
rm bored.py

I want it to confirm before it removes.

(I use konsole btw)

U. Windl avatar
gf flag
While aliasing `rm` to `rm -i` seems like a solution, the better solution is to be careful when entering the command. `rm -i` just gives you a false feeling of safety: Imagine you rely on being asked before each deletion, and (for various reasons) no question is asked, and the deletion is done immediately.
Score:4
my flag

Refer to the man page of rm:

   -i     prompt before every removal

   -I     prompt  once  before  removing more than three files, or when removing recursively.
          Less intrusive than -i, while still giving protection against most mistakes

So, using -i flag with rm will give you a prompt similar to:

$ rm -i bored.py
rm: remove regular file 'bored.py'? 

You can press Y will confirm the removal. Pressing N or any other key will deny the removal.

You can create an alias in your .bashrc or .bash_alias to make this flag permanent:

echo "alias rm='rm -i'" >> ~/.bashrc
source ~/.bashrc

NOTE: This will only work on files when not used with the recursive (-r) or forceful (-f) flag.

Score:4
in flag

You can do this by creating an alias for rm to include the flag for interactive deletes.

Here's how:

  1. Open Konsole (if it's not already open)
  2. Edit the .bash_aliases file:
    {editor of choice} .bash_aliases
    
    Note: Be sure to replace `{editor of choice} with your editor of choice.
  3. Add an alias for rm:
    alias rm="rm -i"
    
  4. Save the file and exit
  5. Reload your profile:
    source ~/.profile
    

Now any time you try to delete a file, you'll see something like this:

$ rm bored.py 
rm: remove regular file 'bored.py'?

Pressing Y will delete the file. Pressing anything else will act as a "No", cancelling the operation.

Note: You can still use other flags despite the alias, so if you plan on removing lots of files, rm -f *.py will still work as expected without prompting for each individual file.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.