What would happen if I did `rm -rf *` in the root directory?

5,738

Solution 1

If you ran the sequence of commands:

cd
rm -rf *

All non-hidden files and directories in your home directory will be deleted. Any contents of any userfs-mounted partitions (networked or otherwise) will be deleted. You may or may not be a very sad panda.

Would "this break the computer"? No. Would it cause you to lose any of your files, personally installed applications, desktop configurations, et cetera? Definitely.

If you did this (with superuser permissions) in the root directory, the results would be catastrophic. Any (non-hidden) files in the root directory, and the contents of all (non-hidden) directories in the entire filesystem would be deleted. Again, this includes the contents of any remotely mounted media (such as that NAS mount in /mnt/media to your collection of TV shows and movies, for instance).

Again - would this "break the computer"? No. Would it render it unusable until a new operating system is installed? Almost definitely unless another (unmounted) bootable partition exists. If you are unfortunate enough to have /boot mounted read-write after boot-time, there may be ramifications reaching to the bootability of other operating systems also.

Don't do this. Even on a VM.

Solution 2

There's a difference between what you ask in the topic (running rm -rf * in the root directory) and what you ask in the question itself (doing it in your home directory), but the answer is the same: Don't do it.

A note on terminology: "ruin a computer" means different things to different people, I've previously f*cked my own desktop in a way so that I needed to press a key and select the right disc to boot from, I was fine with it, my mother would have considered that ruined.

The command would recursively delete everything (except hidden files/direcories), across mounts.

If from your home directory you "only" lose your own data, configuration, etc. and whatever is mounted as a subdirectory (not neccessarily directly) of your home directory (I don't know what "modern" systems/desktop environments do), on my desktop that would often include (parts of) my NAS holding my collection of movies/TV shows et al.

If from the root directory (with proper permissions, if done as an ordinary user, this would in most cases be comparable to if done from your home directory) you would lose everything (see anecdote below), and in this case you would be sure any mount was below, so you would delete everything on any mount.

15 or so years ago I actually tried doing rm -fr * from / as root, on a machine I wanted to reinstall anyway, for some reason the process died part way through the process (so it hadn't deleted everything), but it had deleted enough that the installation couldn't be rescued, and there wasn't any good way of finding out what had happened causing it stop deleting stuff.

I repeat the point: Don't do this!

Share:
5,738

Related videos on Youtube

quanty
Author by

quanty

Updated on September 18, 2022

Comments

  • quanty
    quanty almost 2 years

    I was just thinking about the

    rm -rf *
    

    command. The rm command removes any files following it, the -rf bit indicates to extend the rm command to include directories, and * means everything.

    So, I thought about what would happen If I did:

    cd 
    rm -rf *
    

    Could this ruin a computer? I'm not very well versed with what everything in the root directory does, but it seems like a lot of it essentially runs the computer.

    So what would happen if I did this? How bad would it be? Could it break a computer?

    Further

    As an interesting additional point, are there any commands as basic as this that can be done in the terminal which would be very damaging?

    • Kusalananda
      Kusalananda about 6 years
      Set up a virtual machine with the Unix of your choice and try it in there. There is no better way of learning than by doing (other than possibly by teaching).
    • EDubman
      EDubman about 6 years
      Furthermore, if you were to do this in the root directory and append --no-preserve-root it would be more devastating. But would it impact your usability of hardware? Possibly, on UEFI machines! Read: thenextweb.com/insider/2016/02/01/…
    • C0deDaedalus
      C0deDaedalus about 6 years
      As for any commands as basic that you asked, have you tried typing :(){:|:&};: this at terminal.
  • Pankaj Goyal
    Pankaj Goyal about 6 years
    VMs are easily rolled back. The files on the host system which are mounted in a share are not.
  • Henrik supports the community
    Henrik supports the community about 6 years
    Whether VM's are easily rolled back depends quite a lot on the virtualisation solution used.
  • Cruncher
    Cruncher over 4 years
    "for some reason the process died part way through the process" I've always wondered if the command would ever actually complete. I always figured not. Who knows why it actually stops, but I just don't see how it could possibly complete. How is bash running if its binary doesn't exist? Your entire OS is gone.
  • Henrik supports the community
    Henrik supports the community over 4 years
    bash (or whatever shell you're using) would be in RAM, in that case it shouldn't matter what is on disc.