M BUZZ CRAZE NEWS
// news

How to make confirmation when deleting files via terminal

By Gabriel Cooper
cd Desktop/
rm bored.py

I want it to confirm before it removes.

(I use konsole btw)

1

2 Answers

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.

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy