How to make confirmation when deleting files via terminal
cd Desktop/
rm bored.pyI want it to confirm before it removes.
(I use konsole btw)
12 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 ~/.bashrcNOTE: 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:
- Open Konsole (if it's not already open)
- Edit the
.bash_aliasesfile:
Note: Be sure to replace `{editor of choice} with your editor of choice.{editor of choice} .bash_aliases - Add an alias for
rm:alias rm="rm -i" - Save the file and exit
- 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.