“rm-rf” Windows 的等效项?
我需要一种方法来递归删除文件夹及其子文件夹。
有没有为此预先构建的工具,或者我需要编写一个吗?
DEL /S
不会删除目录。
DELTREE
已从 Windows 2000+ 中删除
I need a way to recursively delete a folder and its children.
Is there a prebuilt tool for this, or do I need to write one?
DEL /S
doesn't delete directories.
DELTREE
was removed from Windows 2000+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
如果您使用的是旧版本的 Windows,还有 deltree。
您可以从这里了解更多信息:
SS64:DELTREE - 删除所有子文件夹和文件。
There is also deltree if you're on an older version of windows.
You can learn more about it from here:
SS64: DELTREE - Delete all subfolders and files.
Windows 注册表编辑器版本 5.00
[HKEY_CLASSES_ROOT\Folder\shell\rmdir\command]
@="cmd.exe /s /c rmdir "%V""
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Folder\shell\rmdir\command]
@="cmd.exe /s /c rmdir "%V""
这对我有用:
尝试减少路径的长度。
ie :: 将指向此类文件的所有文件夹重命名为尽可能小的名称。 说出一个字母的名字。 继续在文件夹层次结构中向上重命名。
这样可以有效地减少路径长度。
现在最后尝试立即删除该文件。
here is what worked for me:
Just try decreasing the length of the path.
i.e :: Rename all folders that lead to such a file to smallest possible names. Say one letter names. Go on renaming upwards in the folder hierarchy.
By this u effectively reduce the path length.
Now finally try deleting the file straight away.
使用 Powershell 5.1
将 logs 替换为要删除的目录名称。
get-childitem 从当前路径 (.) 递归搜索具有该名称的子目录。
remove-item 删除结果。
Using Powershell 5.1
Replace logs with the directory name you want to delete.
get-childitem searches for the children directory with the name recursively from current path (.).
remove-item deletes the result.
使用风险由您自行承担。 信息按“原样”提供。 未经过广泛测试。
右键单击 Windows 图标(通常是左下角)> 单击“Windows PowerShell(管理员)”> 使用此命令(请务必小心,如果不小心,您可以轻松删除所有文件):
其中
somedir
是您要删除的非空目录。请注意,对于外部连接的磁盘或有问题的磁盘,Windows 有时会表现得很奇怪 - 删除(或任何复制尝试)时不会出错,但目录不会按照指示删除(或未复制)。 (我发现在这种情况下,至少对我来说,@n_y 在他的答案中给出的命令会产生类似“get-childitem:文件或目录已损坏且无法读取。”的错误,从而在 PowerShell 中产生错误)
USE AT YOUR OWN RISK. INFORMATION PROVIDED 'AS IS'. NOT TESTED EXTENSIVELY.
Right-click Windows icon (usually bottom left) > click "Windows PowerShell (Admin)" > use this command (with due care, you can easily delete all your files if you're not careful):
Where
somedir
is the non-empty directory you want to remove.Note that with external attached disks, or disks with issues, Windows sometimes behaves odd - it does not error in the delete (or any copy attempt), yet the directory is not deleted (or not copied) as instructed. (I found that in this case, at least for me, the command given by @n_y in his answer will produce errors like 'get-childitem : The file or directory is corrupted and unreadable.' as a result in PowerShell)
您可以安装 GnuWin32 并在 Windows 上本机使用 *nix 命令。 我在 Windows 的薄荷味新副本上安装其他任何东西之前先安装这个。 :)
You can install GnuWin32 and use *nix commands natively on windows. I install this before I install anything else on a minty fresh copy of windows. :)
在 powershell 中,
rm -recurse -force
工作得很好。In powershell
rm -recurse -force
works quite well.对于在 Windows 计算机上安装 npm 软件包时遇到问题的人,迟到但重要的答案如果您看到错误提示“
rm -rf...
”命令未找到。您可以使用 bash cli 在 Windows 上运行 rm 命令。
对于 npm 用户,如果您尝试安装 npm 软件包,则可以将 npm 的配置更改为
npm config set script-shell "C:\Program Files\Git\bin\bash.exe"
有一个使用rm -rf
命令的安装后脚本,您将能够运行该 rm 命令,而无需更改 npm 包中的任何内容或禁用安装后脚本配置。 (例如,styled-components
在安装后脚本中使用rm
命令)如果您只想使用
rm
命令,您可以轻松地使用使用 bash 并传递参数。所以,是的,您可以在 Windows 上使用“rm”命令。
LATE BUT IMPORTANT ANSWER to anyone who is having troubles installing npm packages on windows machine and if you are seeing error saying "
rm -rf...
" command not found.You can use the bash cli to run rm command on windows.
for npm users, you can change the npm's config to
npm config set script-shell "C:\Program Files\Git\bin\bash.exe"
this way if the npm package you are trying to install has a post install script that usesrm -rf
command, you will be able to run thatrm
command without needing to change anything in the npm package or disabling the post install scripts config. (For example,styled-components
usesrm
command in their post install scripts)If you want to just use the
rm
command, you can easily use the bash and pass the arguments.So yes, you can use the 'rm' command on windows.
通过 Powershell
通过命令提示符
https://stackoverflow.com/a/35731786/439130
via Powershell
via Command Prompt
https://stackoverflow.com/a/35731786/439130
接受的答案很好,但假设您安装了 Node,您可以使用节点库“rimraf”更精确地完成此操作,它允许通配模式。 如果您经常使用它(我经常使用),只需全局安装它即可。
然后,例如,我经常使用的一种模式:
或者用于单行,让您避开全局安装,但包动态下载需要稍长的时间:
The accepted answer is great, but assuming you have Node installed, you can do this much more precisely with the node library "rimraf", which allows globbing patterns. If you use this a lot (I do), just install it globally.
then, for instance, a pattern I use constantly:
or for a one-liner that let's you dodge the global install, but which takes slightly longer for the the package dynamic download:
要删除目录(无论是否存在),请使用以下命令:
For deleting a directory (whether or not it exists) use the following:
尝试这个命令:
Try this command:
首先,让我们回顾一下
rm -rf
的作用:在三种情况下,通常会使用
rm -rf
并期望返回0
:我将忽略整个权限问题,但无论如何没有人使用权限或试图拒绝自己对 Windows 中的内容进行写访问(好吧,这只是一个笑话......)。
首先 将
ERRORLEVEL
设置为 0,然后仅删除路径(如果存在),使用不同的命令,具体取决于是否是一个目录。 如果路径不存在,IF EXIST
不会将ERRORLEVEL
设置为 0,因此必须首先将ERRORLEVEL
设置为 0,才能正确检测成功一种模仿正常 rm -rf 用法的方式。 使用IF EXIST
保护RD
是必要的,因为RD
与rm -f
不同,如果目标不存在。以下脚本片段假定 DELPATH 已加引号。 (当您执行诸如
SET DELPATH=%1
之类的操作时,这是安全的。尝试将ECHO %1
放入.cmd
中并向其传递一个参数里面有空格,看看你自己会发生什么)。 代码片段完成后,您可以使用IF ERRORLEVEL 1
检查是否失败。重点是,当环境符合 POSIX 时,一切都会变得更简单。 或者,如果您安装一个最小的 MSYS 并使用它。
First, let’s review what
rm -rf
does:There are three scenarios where
rm -rf
is commonly used where it is expected to return0
:I’m going to ignore the whole permissions thing, but nobody uses permissions or tries to deny themselves write access on things in Windows anyways (OK, that’s meant to be a joke…).
First set
ERRORLEVEL
to 0 and then delete the path only if it exists, using different commands depending on whether or not it is a directory.IF EXIST
does not setERRORLEVEL
to 0 if the path does not exist, so setting theERRORLEVEL
to 0 first is necessary to properly detect success in a way that mimics normalrm -rf
usage. Guarding theRD
withIF EXIST
is necessary becauseRD
, unlikerm -f
, will throw an error if the target does not exist.The following script snippet assumes that DELPATH is prequoted. (This is safe when you do something like
SET DELPATH=%1
. Try puttingECHO %1
in a.cmd
and passing it an argument with spaces in it and see what happens for yourself). After the snippet completes, you can check for failure withIF ERRORLEVEL 1
.Point is, everything is simpler when the environment just conforms to POSIX. Or if you install a minimal MSYS and just use that.
rmdir /S /Q %DIRNAME%
rmdir /S /Q %DIRNAME%
rmdir /s 目录名
rmdir /s dirname
这是您需要做的...
使用以下行创建一个批处理文件
RMDIR /S %1
将批处理文件保存为
Remove.bat
并将其放入 < code>C:\windows创建以下注册表项
HKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)
启动
regedit
并更新默认值HKEY_CLASSES_ROOT\Directory\shell\删除目录 (RMDIR)\default
具有以下值
"c:\windows\REMOVE.bat" "%1"
就是这样! 现在您可以右键单击任何目录并使用 RMDIR 功能
Here is what you need to do...
Create a batch file with the following line
RMDIR /S %1
Save your batch file as
Remove.bat
and put it inC:\windows
Create the following registry key
HKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)
Launch
regedit
and update the default valueHKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)\default
with the following value
"c:\windows\REMOVE.bat" "%1"
Thats it! Now you can right click any directory and use the RMDIR function
在powershell中,rm是
Remove-Item
的别名,所以删除一个文件,相当于
如果你对gnu
rm
util感到满意,你可以rm< /code> util,由 Windows 上的 choco 包管理器提供。
使用 choco 在 powershell 中安装 gnu utils:
最后,
in powershell, rm is alias of
Remove-Item
, so remove a file,is equivalent to
if you feel comfortable with gnu
rm
util, you can therm
util by choco package manager on windows.install gnu utils in powershell using
choco
:finally,
作为旁节点:
从包含所有子目录的 linux 版本(递归)+强制删除
$ rm -rf ./path
到 PowerShell
PS> rm -r -fo ./path
具有接近相同的参数(只是分开)(需要 -fo,因为 -f 可以匹配不同的其他参数)
注意:
As a sidenode:
From the linux version with all subdirs (recursive) + force delete
$ rm -rf ./path
to PowerShell
PS> rm -r -fo ./path
which has the close to same params (just seperated) (-fo is needed, since -f could match different other params)
note:
RMDIR 或 RD 如果您使用经典命令提示符 (cmd.exe):
如果您使用的是 PowerShell,则可以使用
Remove-Item
(其别名为del
、erase
、rd
、ri
、rm
和rmdir
) 并采用-Recurse
参数,该参数可以缩写为-r
RMDIR or RD if you are using the classic Command Prompt (cmd.exe):
If you are using PowerShell you can use
Remove-Item
(which is aliased todel
,erase
,rd
,ri
,rm
andrmdir
) and takes a-Recurse
argument that can be shorted to-r
是您可以在 Windows PowerShell 中获得的最接近的结果。 的缩写。
它是(更多详细信息)
is the closest you can get in Windows PowerShell. It is the abbreviation of
(more details).
你可以安装cygwin,它有
rm
以及ls
等。You can install cygwin, which has
rm
as well asls
etc.转到该路径并触发此命令。
/s :删除指定的目录和所有子目录,包括任何文件。 使用 /s 删除一棵树。
/q :以安静模式运行 rmdir。 删除目录而不确认。
/? :在命令提示符处显示帮助。
Go to the path and trigger this command.
/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.
/q : Runs rmdir in quiet mode. Deletes directories without confirmation.
/? : Displays help at the command prompt.
RMDIR [/S] [/Q] [驱动器:]路径
RD [/S] [/Q] [驱动器: ]path
/S
删除指定目录中除目录本身之外的所有目录和文件。 用于删除目录树。/Q
安静模式,不询问是否可以使用删除目录树/S
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S
Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree./Q
Quiet mode, do not ask if ok to remove a directory tree with/S
管理:
适用于任何内容,包括系统文件
编辑:我实际上找到了也解决文件路径太长问题的最佳方法:
admin:
Works for anything including sys files
EDIT: I actually found the best way which also solves file path too long problem as well: