删除除特定文件扩展名之外的所有文件

Windows

删除当前目录中的所有文件(后缀名log和txt文件除外)

for /f %F in ('dir /b /a-d ^| findstr /vile ".log .txt"') do del "%F"

删除当前目录下的所有文件,包括子级目录下的文件(后缀名log和txt文件除外)

for /f %F in ('dir /s /b /a-d ^| findstr /vile ".log .txt"') do del "%F"

如果要将此命令放在批处理文件中,请在行中将%F更改%% F。

Linux

删除当前目录中的所有文件,后缀名为log的文件除外。

find . -type f ! -iname "*.log" -delete

相关链接

https://docs.microsoft.com/zh-cn/windows-server/administration/windows-commands/dir
https://docs.microsoft.com/zh-cn/windows-server/administration/windows-commands/for

原文地址:https://www.cnblogs.com/RainFate/p/14213966.html