Posted by: Anurag Panda on: May 17, 2008
When I say “sudo rm **” I do mean a specific command which is used to delete folders in a *nix system. If you have visited Ubuntu Forums in the recent months, you would have seen lots of signatures of many users and moderators to anyone to not to use this command unless you are certain what you are doing. Now this is exactly true.
However there’s a problem to those answering questions, Many time we need to tell users how to delete folders which have only acces to root and usually this rm command comes to mind.
However my advice to folks is that DO NOT tell users to use rm command ever. A better way for them is to instruct the user to launch nautilus or konqueror in root access.
sudo nautilus
sudo konqueror
And the chances of user destroying his system will reduce considerably.
I hope in the future the sudo rm ** command should always issue a warning about exactly what it is doing before committing it.
Ex:
$ sudo rm -** abc
This command will remove the directories abc and all the files and directories within it. Commit? (Y/N):
AND
$ sudo rm -** /
THIS WILL WIPE OUT THE ENTIRE SYSTEM AND YOU WONT BE ABLE TO RECOVER. Commit (Y/N):
There’s one big problem with A.N.Onymous’ solution:
aliases don’t work with sudo.
I already have -iv added to mv, cp and rm in my .bashrc through aliases.
However, it only works without sudo.
Putting the aliases in .bashrc for root works only if you log in as root, not with sudo.
Then the solution would be to deactivate sudo and force usage of the root account…
Yes, aliasing rm with sudo is possible! ![]()
Here:
http://ubuntuforums.org/showpost.php?p=221245&postcount=11
Just add this one line to ~/.bashrc and it works:
alias sudo=”sudo ”
All user aliases will now be used when using sudo.
As for rm -rf, I believe the purpose of “-f” is to “ignore nonexistent files, never prompt”. So it should certainly not be made interactive.
I’m not sure wether implementing the sudo alias by default is good because it might have potential security risks since common commands could be replaced by malicious commands in the unprotected ~/.bashrc.
(but if someone can change your ~/.bashrc, he can add it himself, so…)
Your solution of advising the use of nautilus is not such a bad idea.
June 27, 2008 at 10:42 am
Your suggestion is totally impractical – it flies in the face of the Unix tool philosophy. Most Unix commands are made to be able to string together, for automated use in shell scripts and such and so defaults to being non-interactive, assuming that the user knows what they wants.
If commands (rm in this case) suddenly started becoming interactive then you’d instantly break all shell scripts using those commands who will suddenly be prompting in all sort of unpredictable ways (and possibly in contexts where no user is available to answer the prompts.) Imagine a shell script that’s scheduled to run at midnight to remove all temp files (for argument sake.) All of a sudden that will stop working because “rm” suddenly “helpully” decides to prompt about all the files it’s going to be deleting. Not a good idea.
In any case, there’s a way for you to make rm behave the way you want in interactive sessions, which at a stroke solves your problem for you, and retains rm’s default behaviour for all other uses. There’s this thing called “shell aliases”, you can alias “rm” to being “rm -i” which will force rm to prompt before deletion, as you want it to. Enter at a command prompt “:
alias rm=”rm -i”
Now try deleting a file, you’ll see “rm” prompt you before deletion. You can put the above alias command at the end of the “.bashrc” file in your home folder to make it “permanent” for you when you log on.
Finally, please note that “rm *” is identical to “rm **” and that in “rm -** /” the “-**” is pretty meaningless. “rm -**” is the same as “rm -*” and will delete only files starting with “-”.