7

They say rm command defaults to the option --preserve-root. Is that right?

Otherwise I should put the line

alias rm='rm --preserve-root'`

in ~/.bashrc to make that option happen without typing it every time I run the rm command. To confirm this I ran type rm, and got rm is hashed (/bin/rm).

I expected rm is aliased to rm --preserve-root. Does anyone know what's going on with the rm command?

psmears
  • 138
  • 4
Smile
  • 1,099
  • 1
    sorry I have no idea who "they" are, but I think you'll find it depends on your kernel & shell/bash version. I know some of the systems I use (which are still supported) don't have --preserve-root on rm. – guiverc Dec 01 '17 at 12:34
  • Oh, I said they cause I'm reading a book, whose name is like Ubuntu Unlished 2017 or something. I thought saying specific book name makes me look like book seller, so I did like that. My ubuntu version is Ubuntu 16.04. I still want to know how to confirm whether --preserve-root is default or not. – Smile Dec 01 '17 at 12:42
  • 1
    At least for me I get for sudo rm -r / the following message: rm: it is dangerous to operate recursively on '/' and rm: use --flag-i-wont-mention to override this failsafe – Videonauth Dec 01 '17 at 12:44
  • Yeah, I also wanted to test it. However I was afraid it might cause catastrophe. Thanks man. – Smile Dec 01 '17 at 12:53
  • 1
    This is also present in the manpage for rm. –  Dec 01 '17 at 12:57
  • Yeah, but the thing was when I asked for confirmation on command line by typing "type rm" it was not what is expected like the question. Anyhow Dan cleared everything. Thanks guys. – Smile Dec 01 '17 at 12:59
  • 1
    @guiverc It has nothing to do with the kernel, or your shell (bash or otherwise). It's a default option in GNU rm since some years, which is a separate program from your shell. (It's in coreutils in Debian and Ubuntu). – marcelm Dec 01 '17 at 13:46
  • rm -rf / is almost always executed unintentionally so they added a safeguard. – Joshua Dec 01 '17 at 16:41
  • 2
    For those who are wondering, the '--preserve-root' was made the default for rm in core-utils on 2016-09-02 in: http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=89ffaa19;hp=963d29f9 – qneill Mar 28 '18 at 19:01
  • 1
    See also info rm, specifically https://www.gnu.org/software/coreutils/manual/html_node/Treating-_002f-specially.html for some background (generally GNU commands have more details in info than in man pages). As it explains, some related commands like chmod kept the historic --no-preserve-root behavior. – Beni Cherniavsky-Paskin Dec 02 '19 at 12:23
  • 1
    @qneill you mean 2006 – ychaouche Sep 15 '21 at 12:28
  • 1
    Dang @ychaouche yeah good catch, wish I could edit comments, it was kind of the whole point :). Note to self: future proof your comments. – qneill Sep 15 '21 at 21:57

1 Answers1

10

rm is not being aliased to rm --preserve-root, but the option is selected by default in the rm binary.

From the manpage of rm in Ubuntu 17.10, you can find the following details of the --preserve-root and --no-preserve-root options:

   --no-preserve-root
          do not treat '/' specially

   --preserve-root
          do not remove '/' (default)
Dan
  • 13,119
  • I have read man page, but I totally missed the "(default)". Thanks for the reply. BTW when you mention 'rm binary', did you mean rm is programmed in that way?? – Smile Dec 01 '17 at 12:56
  • @Smile Yes, that's what it means. –  Dec 01 '17 at 12:57