Powershell Command: rm -rf
Asked Answered
R

3

91

rm is to remove item, but what is the parameter -rf do or signify?


Whenever I typed help -rf it printed the entire list of available commands in powershell. What happens if you type rm -rf in powershell? From reading around I've gathered that it will delete everything on the drive? I'm not sure?

Also, is rm -rf same as rm -rf /?

Ragi answered 4/5, 2012 at 6:36 Comment(0)
H
164

PowerShell isn't UNIX. rm -rf is UNIX shell code, not PowerShell scripting.

  • This is the documentation for rm (short for Remove-Item) on PowerShell.
  • This is the documentation for rm on UNIX.

See the difference?

On UNIX, rm -rf alone is invalid. You told it what to do via rm for remove with the attributes r for recursive and f for force, but you didn't tell it what that action should be done on. rm -rf /path/to/delete/ means rm (remove) with attributes r (recursive) and f (force) on the directory /path/to/remove/ and its sub-directories.

The correct, equivalent command on PowerShell would be:

rm C:\path\to\delete -r -fo

Note that -f in PowerShell is ambiguous for -Filter and -Force and thus -fo needs to be used.

Harlin answered 4/5, 2012 at 6:41 Comment(6)
For completeness, the PowerShell equivalent would be rm \ -r -f. You can abbreviate parameter names in PowerShell, but you can't stack them.Bergman
This answer is incorrect. If you do rm <path> -r -f in PowerShell, but the path does not exist, an exception is thrown. In unix, if you run rm -rf <path> on a path which does not exist, the command exits successfully. The two commands are not equivalent.Oakum
The PowerShell equivalent is (no longer?) true. If you do that you will receive an error Remove-Item : Parameter cannot be processed because the parameter name 'f' is ambiguous. Possible matches include: -Filter -Force. You can use rm C:\path\to\delete -r -fo however.Potvaliant
The PowerShell command does not work. When I try rm path\to\delete -r -fo I get error message: Remove-Item: Cannot remove item path\to\delete: The directory is not empty. : 'path\to\delete' I am using PS version 7.1.1Diedrediefenbaker
I found out why (where) the PS command does not work. If I try it in a folder handled by OneDrive it fails, but it works otherwise. Microsoft please fixit!Diedrediefenbaker
and if (when) you get permissions errors, see this post: #43768293 (shut down applications that may have a lock on the files/folders you're trying to delete).Annular
L
50

You have to use:

Remove-Item C:\tmp -Recurse -Force

or (short)

rm C:\tmp -Recurse -Force
Lombok answered 4/5, 2012 at 6:43 Comment(2)
So.. waht does rm -rf / does in powershell?Ragi
Gotcha, you didn't specify at first, so I wasn't sure.Ragi
F
7

This is the one-liner that behaves like rm -rf. It first checks for the existence of the path and then tries removing it.

if (Test-Path ./your_path) { rm -r -force ./your_path}
Flat answered 24/10, 2021 at 21:41 Comment(1)
Thank you that's the right answer I was looking for. Many people forget that UNIX's -f actually allows to silently ignore a nonexistent directory.Savour

© 2022 - 2024 — McMap. All rights reserved.