I often misuse "rm" for "mv"! I hope to make "rm" a command requiring root privilege, like "apt-get". How to do that, please? My system is Ubuntu 10.10.
6 Answers
While you could use chmod to give only the root user execute permissions, this would likely create many problems on the system. A better choice would be to alias the command in your .bashrc file by adding the following line:
alias rm="rm -i"
This makes the command interactive and you will then be asked to confirm each deletion.
Thanks to other posters for pointing this out.
-
Exactly it is !– updogliuFeb 16, 2012 at 7:44
-
11-1 This approach will wreck any system command which invokes
/bin/rm
behind the scenes (basically any shell script which uses temporary files, among others)! Go with the alias instead.– tripleeeFeb 16, 2012 at 9:01 -
4
-
3
rm
needs to be usable by applications which created their own files, are owned by them and shouldn't need root priveleges to remove them. Userm -i
Feb 16, 2012 at 10:12 -
1Normally I wouldn't edit an accepted answer, but I've learned a lot over the years and having just come across this again today I'm a bit horrified about what I originally wrote. The original answer is still contained, but with a warning and the better suggestions, with thanks.– IlionJul 28, 2016 at 22:32
Definitely aliasing the interactive version of rm
is the best solution, because you wouldn't want normal scripts to break when rm
suddenly starts to behave differently. Just add alias rm="rm -i"
to your ~/.bashrc
and you'll get a question like rm: remove regular file 'filename'?
, which you'll have to answer with y [ENTER]
or n [ENTER]
.
I think it is better that aliasing sudo rm
, because you are explicitly told which files are to be deleted, while with sudo you're only asked for a password like on dozen other occasions and before you know it you will approve the command sudo rm *
. The command rm -i
protects you from such (and less obvious) too greedy wildcard matches, because it asks you about every single file.
How about alias rm to 'sudo rm'? Every time you need to enter the password.
-
4You hardly want to run
rm
with escalated privileges. I would go for something likealias rm='sudo true && /bin/rm’
or just the good oldrm -i
. Withsudo
you get to run without the password for a while, whereasrm -i
will always prompt.– tripleeeFeb 16, 2012 at 10:08
You could alias rm
to something like echo 'Do you really want to remove this?'
This will prevent you from using rm.
Then, if you really want to remove something you will need to type /bin/rm
(to bypass the alias).
This is off the top of my head. I'm sure folks will correct edit if I have my syntax off or something.
Insert this script in your ~/bin and call it rm.
#!/bin/sh
# Run /bin/rm using all the arguments from the command line
/bin/rm $@
Make sure that ~/bin is first in your $PATH so your rm is found before /bin/rm.
Set that script to ownership root:your_group and permissions 760 so that you must sudo in order to execute rm and you will not need to be root to write or read the file. You can then also get around this script by simply using /bin/rm instead of rm, but you'll do that knowingly. Maybe you'll even get in the habit of specifying rm by absolute path and eventually will do without the little extra script.
If you aren't familiar with setting your own path variable, edit your .bash_profile with these lines (or edit if they already exist).
PATH=~/bin:$PATH
export PATH
You'll need to restart bash or use the command 'source .bash_profile' to bring your changes in to the current shell.
(Of course, rm -i
is much simpler and less breaky than chmoding rm itself, but this answers the question as asked without breaking anything that isn't run by the user who already knows of the limitation.)
rm
:'rm -i'
rm
rm
when you meanmv
, and don't have backups or a versioning system (git-scm.com) or at least a recycle bin or something (pages.stern.nyu.edu/~marriaga/software/libtrash), you kind of deserve to lose files.