如何在shell脚本中删除带有一些异常文件夹的空文件夹?
例如: 我有文件夹keep1、子文件夹keep2和文件夹keep2的子文件夹keep3:如何在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(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-可能在不同的位置有多个文件夹,每次删除一个文件夹时创建文件夹会很麻烦。