Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在shell脚本中删除带有一些异常文件夹的空文件夹?_Shell - Fatal编程技术网

如何在shell脚本中删除带有一些异常文件夹的空文件夹?

如何在shell脚本中删除带有一些异常文件夹的空文件夹?,shell,Shell,例如: 我有文件夹keep1、子文件夹keep2和文件夹keep2的子文件夹keep3: Keep1(not empty) Keep2(not empty) Keep3(empty) 目前我正在使用: for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i" 这将删除所有空文件夹,但我不想删除某些文件夹。在上面的示例中,我想保留空文件夹keep3。您不能使用find吗 for /f "delims

例如: 我有文件夹keep1、子文件夹keep2和文件夹keep2的子文件夹keep3:

Keep1(not empty)
    Keep2(not empty)
        Keep3(empty)
目前我正在使用:

for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i"

这将删除所有空文件夹,但我不想删除某些文件夹。在上面的示例中,我想保留空文件夹keep3。

您不能使用find吗

for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r ^| find /V "Keep3"') do rd "%%i"
?

编辑 我看到您可能需要跳过多个位置,因此可以使用findstr

如果你必须忽略Keep3和Keep4,那么

for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r ^| findstr /V "Keep3 Keep4"') do rd "%%i"

它也是基本的正则表达式功能

为什么不在删除folderIs Perl选项后执行mkdir呢?File::Find相当不错。@code frenzy-可能在不同的位置有多个文件夹,每次删除一个文件夹时创建文件夹会很麻烦。