4

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.

5
  • This is off-topic for StackOverflow. It should be asked on serverfault.com or askubuntu.com
    – Brian Roach
    Feb 16, 2012 at 7:40
  • 3
    Create a shell alias for rm: 'rm -i'
    – Brett Hale
    Feb 16, 2012 at 8:17
  • 1
    You could also alias rm to be "sudo rm" Feb 16, 2012 at 9:18
  • 3
    It's not the answer you want, but you should take care and think before pressing enter, especially around dangerous commands like rm
    – CJBrew
    Feb 16, 2012 at 9:34
  • 3
    I agree with @CJBrew. And i think if you keep typing rm when you mean mv, 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. Feb 16, 2012 at 16:15

6 Answers 6

-3

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.

5
  • Exactly it is !
    – updogliu
    Feb 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.
    – tripleee
    Feb 16, 2012 at 9:01
  • 4
    This is likely to break things. Feb 16, 2012 at 9:17
  • 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. Use rm -i
    – deed02392
    Feb 16, 2012 at 10:12
  • 1
    Normally 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.
    – Ilion
    Jul 28, 2016 at 22:32
12

The best solution is to use rm -i

5

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.

3

How about alias rm to 'sudo rm'? Every time you need to enter the password.

1
  • 4
    You hardly want to run rm with escalated privileges. I would go for something like alias rm='sudo true && /bin/rm’ or just the good old rm -i. With sudo you get to run without the password for a while, whereas rm -i will always prompt.
    – tripleee
    Feb 16, 2012 at 10:08
1

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).

1

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.)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.