Rm is the most common command used by Linux users to delete files & folders. By default, rm does not ask for a confirmation before file deletion. Often you may need to need to enable confirmation for rm command in Linux. This is required by system administrators so that their users do not accidentally delete important stuff from their systems. Here are the steps to do so.
How to Enable Confirmation for Rm Command
Here are the steps to enable confirmation for rm command.
Open terminal, and enter the following command. This will open .bashrc file in vi editor.
$ vi ~/.bashrc
Add the following line to it.
alias rm=’rm –I’
In the above command, we have created an alias that calls ‘rm -I’ command whenever user runs rm command. ‘rm -I’ command runs rm command in interactive mode and asks for confirmation before deleting files & folders.
Save and close the file. Run the following command to apply changes.
$ source ~/.bashrc
The above file will only make changes for the current user.
If you want to add confirmation prompt for rm command for another user, you need to login as that user (or use su command to switch user) and then repeat the above steps.
If you want to make this change for all users, you need to add the above mentioned alias command to file /root/.bashrc. You may need to use sudo command to be able to open this. Also, you may be asked for your account password before command execution.
$ sudo vi /root/.bashrc
Once you add the above mentioned alias command command to this file, save and close it. Run the following command to apply changes.
$ source /root/.bashrc
Now when you delete a file using rm command, you will get a confirmation prompt.
$ rm /home/ubuntu/data.txt rm: remove regular file '/home/ubuntu/data.txt'? y
Even when you enter ‘rm -r’ command to recursively delete folder, you will get a confirmation prompt.
In this article, we have learnt how to enable confirmation for rm command. It is a simple way to prevent users from accidentally deleting files & folders on their system.
Please note, if you enable confirmation prompt for rm command, then even when you call the command from within shell script, it will show a prompt, and may pause the execution until it gets a response.
In fact, you can use this method of defining aliases to define custom commands, as well as modify the default behavior of other commands.