8

I wanted to delete files and folder recursively from a particular folder so I ran this command from that folder

rm -rf *

I assumed it would delete all files/directories under the current directory recursively. Something bad happened after that (my server is down, not even getting ping response).

5
  • 1
    Without knowing where exactly you executed this command, it's hard to say why that is, but it seems that you deleted something that your system needs to function.rm -rf * does pretty much as you say (excluding the fact that that it ignores hidden files). In general, it's a better idea to go up one directory and rm -r the directory itself, recreating it if necessary.
    – Chris Down
    Dec 20, 2013 at 11:21
  • I just wanted to know if it deletes files/folder recursivly under current directory or root?
    – Windows
    Dec 20, 2013 at 11:35
  • 3
    It does so only under the current directory. Even if you entered .*, POSIX doesn't allow listing ...
    – Chris Down
    Dec 20, 2013 at 11:38
  • 3
    Even rm -Rf --no-preserve-root / will typically leave the machine pingable. The kernel runs from RAM, it continues to run even if the filesystem(s) are destroyed.
    – derobert
    Dec 20, 2013 at 12:05
  • 1
    rm -rf will never delete . or .., even if explicitly told to. See here.
    – terdon
    Dec 20, 2013 at 13:32

1 Answer 1

12

Don't forget the possibility that the server being unreachable after the rm command had nothing to do with that. It could be a coincidence!

Most likely though, the current working directory was not what you thought, when the command was issued.

Were you root when doing this?

This is what happens when issuing the command rm -rf *:

  1. The shell resolves the wildcard patterns (* in this case) to all files (including directories, symbolic links and device special files) that matches the used globbing pattern, in this case everything not beginning with a . Normally, these are sorted "alphabetically" by the shell.
  2. The shell then forks a new process and exec()s the first version of rm found in your $PATH, with -rf as the first argument, and the matched files, one by one as the consecutive arguments.

If the rm that was invoked was the standard rm command, it first parses the arguments, one by one, treating all arguments (including ones resulting from shell globbing) that begin with a - as options until it comes to an argument that does not begin with a - (except for commands using GNU getopt() that accept options after non-options), or one that is exactly --. Everything after that is considered file names.

In other words, if you had a file called (for example) --no-preserve-root in the current directory, that would have been interpreted as an option to rm, not as a file name to remove!

Be careful with wildcards (and with file naming). If you have a file called -r in the current directory, the command ls * would list the other files in reverse order, and not show the '-r' file.

In other words, if you had a file called --no-preserve-root, that would have been passed as an option to rm, not as a file name.

Use rm -rf -- * to prevent that from happening, and also remove the files beginning with -. Or simply use absolute or relative path names to make the filenames begin with something other than -, as in: rm ./--flagfile or rm /abs/path/to/--flagfile.

There is also a -i flag to rm that makes it prompt ("interactively") before removing anything.

2
  • 2
    Note that -- is a GNU feature which may not be present in all rm versions. The other way to remove the file called -r is to rm ./-r. I suggest mentioning this in your answer.
    – derobert
    Dec 20, 2013 at 12:06
  • 3
    @derobert, no. -- is POSIX, not GNU. Dec 20, 2013 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .