17

I recently ran rm -rf, not rm -rf /, but nothing happened. I just get a result like this:

Terminal

I was scared because I am worried that it could've deleted some files, but it didn't. Just be sure, could this have deleted any files from my directory?

16
  • 38
    If you are not sure (certain 100%) then don't run a command.
    – Solar Mike
    Aug 4, 2017 at 11:36
  • 5
    Never type commands you do not understand. All you needed was a . or a / and you're gonna be deleting a LOT of stuff.
    – Nelson
    Aug 4, 2017 at 16:33
  • 18
    If you want to experiment with potentially dangerous commands, consider installing a virtual machine. Aug 4, 2017 at 16:36
  • 6
    Experiment all you want (as long as your data is safely backed up). It's only software and you can always reinstall it if anything goes wrong. Don't let anyone train you to be afraid of the computer.
    – hobbs
    Aug 4, 2017 at 20:21
  • 3
    Absolutely do run a command if you're not certain 100%. There are only so many ways to learn, and actually running them is one of them. Caveat emptor and do have backups, obviously, and do use VMs and all that. But not running anything out of fear will do little to help you grow. It should be obvious when to be extra careful (i.e., rm), but for many/most other commands, e.g. all pipe-based text manipulation commands, just trying them often is the easiest/simplest you can do. Together with studying the man page, obviously.
    – AnoE
    Aug 4, 2017 at 21:01

4 Answers 4

51

No, rm -rf will not delete any files because you did not supply an argument to the command.

1
  • 7
    And because of -f, it's not an error to run it with no args. rm -r will complain about "missing operand", but -f suppresses that, and also suppresses errors for files that don't exist. (So for example a script could use rm -rf /foo/bar/* to empty a directory that might already be empty, without having to 2>/dev/null to squash errors.) Aug 6, 2017 at 22:25
36

From the manual page:

rm removes each specified file.

This means you can use it to remove a list of files at once, e.g. with

rm -rf test1.txt test2.txt

Fortunately, all you did was pass an empty list of files, so it deleted nothing. Also, what @SolarMike says: if you don't know what a command does, don't run it. macOS is designed to 'hide' all dangerous (but potentially powerful) Unix operations from the end user.

8
  • 7
    FYI the -r flag is unnecessary unless you are deleting directories recursively. If it's just files, rm is sufficient -- the -f is for force do it doesn't ask you "are you sure" for every file.
    – Tim S.
    Aug 4, 2017 at 16:07
  • 9
    Also, the -f leads to the usage message not being displayed when you use it wrongly. This is why there was no output at all when the OP ran rm -rf. Had they run 'rm -r' without specified files/directories, they would have gotten this usage message: JanNash ~ $ usage: rm [-f | -i] [-dPRrvW] file ... unlink file
    – Jan Nash
    Aug 4, 2017 at 16:47
  • 2
    @barrycarter That is entirely not true - it will still attempt to delete /, it will just skip the nonexistent file xyz* (which will simply not expand because of the lack of a match).
    – fluffy
    Aug 6, 2017 at 0:30
  • 3
    @barrycarter That behavior depends on your shell's globbing configuration. At least in bash's default on macOS, it does in fact delete a and b for me - even without specifying -f. Do the same commands with echo rm -f a b c* instead to see the actual behavior.
    – fluffy
    Aug 6, 2017 at 1:55
  • 2
    @barrycarter and fluffy: What happens depends on if the shell variable nonomatch is set or not. Aug 6, 2017 at 13:31
13

For the layman/Linux/Unix newbie:

rm alone doesn't do anything because you haven't told it what to get rid of.

man rm can explain most of this, if you understand it.

-r means recursive, as in "include everything in subfolders"

-f means force, "don't ask me to confirm" mode

rm -rf(DON'T DO THIS)/ would say delete everything under / (the root folder) without checking (on recent macOS versions SIP will prevent you from removing macOS itself by this, but a lot of other stuff will get deleted)

rm [some file name] would just delete that file.

rm -rf /home/myuser/books would delete everything in myuser's books folder, as well as the folder.

5
  • 6
    Nowadays you need to do rm -rf / --no-preserve-root to shoot your leg correctly. Aug 5, 2017 at 14:43
  • 1
    @Gallifreyan Is there ever a way to shoot your leg correctly? Aug 5, 2017 at 19:32
  • 1
    This one is said to work well. I know no other :) Aug 5, 2017 at 19:34
  • 1
    @Gallifreyan on macOS? pastebin.com/aVAsNcKC indicates otherwise
    – nohillside
    Aug 6, 2017 at 10:59
  • @Gallifreyan: find / -delete should "work" (i.e. actually delete things, so don't run it.) Aug 6, 2017 at 22:28
4

No but if you want to delete Here's an example:

After you launch Terminal (in your /Applications/Utilities folder) type cd ~/Desktop to navigate to the Desktop directory. If you had a file here named MyFile.rtf that you never, ever wanted to see again, you could run this command:

rm MyFile.rtf

When you press Return, the file will go poof! It will be gone, toast, history. You can’t get it back.

2
  • Actually rm removes all reference of the file, doesn't scrub the hard drive of the file by overwriting each byte, if I'm understanding the documentation correctly.
    – NoBugs
    Aug 6, 2017 at 14:23
  • @NoBugs But in fairness this is how all modern 'deletes' work, Windows included. If the delete feature is overwriting each byte then it'll be advertised as such (mainly because it takes longer and is unnecessary most of the time).
    – Insane
    Aug 6, 2017 at 22:44

You must log in to answer this question.