AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1694686
Accepted
Artem S. Tashkinov
Artem S. Tashkinov
Asked: 2021-12-18 17:51:11 +0800 CST2021-12-18 17:51:11 +0800 CST 2021-12-18 17:51:11 +0800 CST

如何安全地递归删除 Windows 中的目录?

  • 772

NTFS 支持各种链接,包括联结、硬链接、软链接等。所以这里有一个问题。

假设您正在递归删除一个实际包含上述内容的目录。很容易想象,您的应用程序,无论是什么,都不是删除所有的联结、可能导致您感兴趣的目录之外的软链接等,而是遍历它们并首先删除其中的所有文件。

这很容易导致重大数据丢失。

这是一个简单的例子。您安装了具有另一个 Windows 安装的硬盘驱动器。

让我们检查一下内容D:\ProgramData:

D:\ProgramData>dir /a
 Volume in drive D has no label.
 Volume Serial Number is 1234-4321

 Directory of D:\ProgramData

12/06/2021  12:56 PM    <DIR>          .
12/06/2021  12:56 PM    <DIR>          ..
07/14/2009  10:08 AM    <JUNCTION>     Application Data [C:\ProgramData]
07/14/2009  10:08 AM    <JUNCTION>     Desktop [C:\Users\Public\Desktop]
07/14/2009  10:08 AM    <JUNCTION>     Documents [C:\Users\Public\Documents]
07/14/2009  10:08 AM    <JUNCTION>     Favorites [C:\Users\Public\Favorites]
02/11/2016  03:51 PM    <DIR>          Microsoft
07/10/2019  03:00 AM    <DIR>          Microsoft Help
12/23/2019  04:04 PM    <DIR>          Package Cache
07/14/2009  10:08 AM    <JUNCTION>     Start Menu [C:\ProgramData\Microsoft\Windows\Start Menu]
07/14/2009  10:08 AM    <JUNCTION>     Templates [C:\ProgramData\Microsoft\Windows\Templates]

现在,如果您尝试递归删除D:\ProgramData怎么办?我什至不会尝试这样做,因为我担心C:\ProgramData会先被删除。

在 Linuxrm -rf中完美地处理了这个 - 它看到符号链接(到目录)并将符号链接作为文件删除,而不试图遍历它们。

在 Windows 中递归删除此类目录的安全方法是什么?

rmdir /q /s? 还有什么?

symbolic-link ntfs
  • 3 3 个回答
  • 306 Views

3 个回答

  • Voted
  1. mashuptwice
    2021-12-21T06:02:16+08:002021-12-21T06:02:16+08:00

    rmdir 将删除符号链接。

    del 将删除链接目标,但不会删除链接。

    rmdir /S /Q将递归删除目录并且不会遵循符号链接。

    我已经在 Windows 10 中测试并确认了 cmd 的行为:

    #create directory structure
    C:\Users\username\test>mkdir 1 2 3
    
    C:\Users\username\test>mkdir 1\testdir
    
    C:\Users\username\test>mkdir 1\testdir\1
    
    #create symbolic link to directory
    
    C:\Users\username\test>mklink /D testlink 1\testdir\1
    symbolische Verknüpfung erstellt für testlink <<===>> 1\testdir\1
    
    
    C:\Users\username\test>dir
    
     Verzeichnis von C:\Users\username\test
    
    28.12.2021  20:24    <DIR>          .
    28.12.2021  20:24    <DIR>          ..
    28.12.2021  20:23    <DIR>          1
    28.12.2021  20:23    <DIR>          2
    28.12.2021  20:23    <DIR>          3
    28.12.2021  20:24    <SYMLINKD>     testlink [1\testdir\1]
                   0 Datei(en),              0 Bytes
                   6 Verzeichnis(se), 22.185.267.200 Bytes frei
    
    #test rmdir with the named parameters
    C:\Users\username\test>rmdir /s /q testlink
    
    C:\Users\username\test>dir
    
     Verzeichnis von C:\Users\username\test
    
    28.12.2021  20:25    <DIR>          .
    28.12.2021  20:25    <DIR>          ..
    28.12.2021  20:23    <DIR>          1
    28.12.2021  20:23    <DIR>          2
    28.12.2021  20:23    <DIR>          3
                   0 Datei(en),              0 Bytes
                   5 Verzeichnis(se), 22.185.267.200 Bytes frei
    
    
    
    C:\Users\username\test>cd 1\testdir\
    
    #obviously the subdirectory in the linked-to directory is still there
    C:\Users\username\test\1\testdir>dir
    
    
     Verzeichnis von C:\Users\username\test\1\testdir
    
    28.12.2021  20:23    <DIR>          .
    28.12.2021  20:23    <DIR>          ..
    28.12.2021  20:24    <DIR>          1
                   0 Datei(en),              0 Bytes
                   3 Verzeichnis(se), 22.184.206.336 Bytes frei
    

    根据此评论,如果从 powershell 调用 rmdir 与从 cmd 调用,rmdir 的行为会有所不同,但是我没有使用 powershell 对其进行测试。

    • 2
  2. harrymc
    2021-12-21T09:45:16+08:002021-12-21T09:45:16+08:00

    要删除所有不是联结的文件,这个命令应该这样做:

    del /s /a:-l
    

    在哪里:

    • /s: 递归
    • /a:-l: 只有不是重解析点的文件

    NTFS 重解析点 包括目录连接、符号链接和卷安装点。

    可以使用以下命令对将要删除的内容进行小测试:

    dir /s /a:-l
    

    (如果它很重要,我仍然会备份该文件夹。)

    • 2
  3. Best Answer
    barlop
    2021-12-29T11:59:21+08:002021-12-29T11:59:21+08:00

    从命令提示符尝试使用 rmdir /s

    我创建了一个目录 c:\abc1,其中包含一个文本文件。

    我创建了一个目录 c:\test1 和一个子目录 c:\test1\blah

    在 c:\test1\blah 我尝试创建指向 c:\abc1 的链接、符号链接或联结链接..

    然后当我在 blah 上执行 rmdir /s 时,我检查了 c:\abc1 并且它的内容仍然存在。

    因此,在 Windows 7 中测试的 cmd 提示符下的 rmdir /s 是安全的。

    C:\>dir c:\abc1
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of c:\abc1
    
    28/12/2021  19:45    <DIR>          .
    28/12/2021  19:45    <DIR>          ..
    28/12/2021  19:45                 6 a.txt
                   1 File(s)              6 bytes
                   2 Dir(s)  137,425,338,368 bytes free
    
    C:\>cd test
    The system cannot find the path specified.
    
    C:\>cd test1
    
    C:\test1>dir
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1
    
    28/12/2021  19:45    <DIR>          .
    28/12/2021  19:45    <DIR>          ..
    28/12/2021  19:46    <DIR>          blah
                   0 File(s)              0 bytes
                   3 Dir(s)  137,425,338,368 bytes free
    
    C:\test1>cd blah
    
    C:\test1\blah>dir
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1\blah
    
    28/12/2021  19:46    <DIR>          .
    28/12/2021  19:46    <DIR>          ..
                   0 File(s)              0 bytes
                   2 Dir(s)  137,425,338,368 bytes free
    
    C:\test1\blah>mklink /?
    Creates a symbolic link.
    
    MKLINK [[/D] | [/H] | [/J]] Link Target
    
            /D      Creates a directory symbolic link.  Default is a file
                    symbolic link.
            /H      Creates a hard link instead of a symbolic link.
            /J      Creates a Directory Junction.
            Link    specifies the new symbolic link name.
            Target  specifies the path (relative or absolute) that the new link
                    refers to.
    
    C:\test1\blah>mklink /d c:\abc1
    The syntax of the command is incorrect.
    Creates a symbolic link.
    
    MKLINK [[/D] | [/H] | [/J]] Link Target
    
            /D      Creates a directory symbolic link.  Default is a file
                    symbolic link.
            /H      Creates a hard link instead of a symbolic link.
            /J      Creates a Directory Junction.
            Link    specifies the new symbolic link name.
            Target  specifies the path (relative or absolute) that the new link
                    refers to.
    
    C:\test1\blah>mklink /d qq c:\abc1
    symbolic link created for qq <<===>> c:\abc1
    
    C:\test1\blah>dir
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1\blah
    
    28/12/2021  19:47    <DIR>          .
    28/12/2021  19:47    <DIR>          ..
    28/12/2021  19:47    <SYMLINKD>     qq [c:\abc1]
                   0 File(s)              0 bytes
                   3 Dir(s)  137,425,469,440 bytes free
    
    C:\test1\blah>dir qq
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1\blah\qq
    
    28/12/2021  19:45    <DIR>          .
    28/12/2021  19:45    <DIR>          ..
    28/12/2021  19:45                 6 a.txt
                   1 File(s)              6 bytes
                   2 Dir(s)  137,425,469,440 bytes free
    
    C:\test1\blah>cd ..
    
    C:\test1>rmdir /s blah
    blah, Are you sure (Y/N)? y
    
    C:\test1>dir c:\abc1
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of c:\abc1
    
    28/12/2021  19:45    <DIR>          .
    28/12/2021  19:45    <DIR>          ..
    28/12/2021  19:45                 6 a.txt
                   1 File(s)              6 bytes
                   2 Dir(s)  137,425,379,328 bytes free
    
    C:\test1>cd blah
    The system cannot find the path specified.
    
    C:\test1>md blah
    
    C:\test1>dir
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1
    
    28/12/2021  19:47    <DIR>          .
    28/12/2021  19:47    <DIR>          ..
    28/12/2021  19:47    <DIR>          blah
                   0 File(s)              0 bytes
                   3 Dir(s)  137,424,666,624 bytes free
    
    C:\test1>cd blah
    
    C:\test1\blah>dir
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1\blah
    
    28/12/2021  19:47    <DIR>          .
    28/12/2021  19:47    <DIR>          ..
                   0 File(s)              0 bytes
                   2 Dir(s)  137,424,666,624 bytes free
    
    C:\test1\blah>mklink /?
    Creates a symbolic link.
    
    MKLINK [[/D] | [/H] | [/J]] Link Target
    
            /D      Creates a directory symbolic link.  Default is a file
                    symbolic link.
            /H      Creates a hard link instead of a symbolic link.
            /J      Creates a Directory Junction.
            Link    specifies the new symbolic link name.
            Target  specifies the path (relative or absolute) that the new link
                    refers to.
    
    C:\test1\blah>mklink /J ww c:\abc1
    Junction created for ww <<===>> c:\abc1
    
    C:\test1\blah>dir
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1\blah
    
    28/12/2021  19:48    <DIR>          .
    28/12/2021  19:48    <DIR>          ..
    28/12/2021  19:48    <JUNCTION>     ww [c:\abc1]
                   0 File(s)              0 bytes
                   3 Dir(s)  137,424,715,776 bytes free
    
    C:\test1\blah>dir ww
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of C:\test1\blah\ww
    
    28/12/2021  19:45    <DIR>          .
    28/12/2021  19:45    <DIR>          ..
    28/12/2021  19:45                 6 a.txt
                   1 File(s)              6 bytes
                   2 Dir(s)  137,424,715,776 bytes free
    
    C:\test1\blah>cd ..
    
    C:\test1>rmdir /s blah
    blah, Are you sure (Y/N)? y
    
    C:\test1>dir c:\abc1
     Volume in drive C has no label.
     Volume Serial Number is 4645-5DCE
    
     Directory of c:\abc1
    
    28/12/2021  19:45    <DIR>          .
    28/12/2021  19:45    <DIR>          ..
    28/12/2021  19:45                 6 a.txt
                   1 File(s)              6 bytes
                   2 Dir(s)  137,424,740,352 bytes free
    
    C:\test1>
    

    真的,您应该能够非常轻松地自己进行测试。

    一条评论提到 powershell rmdir 是不同的。我不能对 powershell 发表太多评论,我不怎么使用它。我看到 powershell 的 rmdir 确实给出了不同的输出,例如 rmdir 说“命令管道中的 cmdlet Remove-Item ...”所以似乎与 cmd 非常不同,我不熟悉它。

    • 0

相关问题

  • 你能启动 NTFS 闪存驱动器吗?

  • WinSCP 中的符号链接文件传输速率

  • Windows 上的 tar 无法从存档中提取符号链接

  • 如何创建脚本来监听内容变化并一直接触父文件夹?

  • 无法创建文件名中带有“:”的新文件,但我已经有 13300 个可以正常工作(linux、ntfs 分区)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve