现在我正在使用
rm -r /blaa/*
删除 blaa
目录中的所有文件夹和文件。我正在寻找一种方法来删除 blaa
目录中的所有文件夹和文件,除非该文件夹名为 abc
。
有什么想法吗?
在Linux中:
有很多方法可以做到这一点;但我相信最好的方法是简单地使用“
find
”工具。
find ! -iname "abc" -exec rm -rf {} \;
我们可以轻松找到并删除所有不名为“abc”的文件和文件夹。
find - to find files
! -iname - to filter files/folders, the "!" means not
-exec - to execute a command on every file
rm -rf - remove/delete files -r for folders as well and -f for force
"{} \;" - allows the commands to be used on every file
在Android中:
由于您无法使用“
rm -rf
”,当您使用“rm -r
”时,它将删除文件夹“.
”,最终删除所有内容。
我猜你的手机已经“root”了,因为你可以使用“查找”工具。
find ! -iname "abc" | sed 1d | xargs rm -r
find - to find files
! -iname - to filter files/folders, the "!" means not
| - pipe sends data to next command
sed - replace text/output
"1d" - removes first line when you do "find ! -iname" by itself
xargs - runs commands after pipe
rm -r - remove/delete files, "-r" for recursive for folders
编辑:在 Android 中修复并测试
您可以轻松更改此设置以满足您的需求,请告诉我这是否有帮助!
...最后万岁...这就是适用于用例的内容(也有助于总结下面的评论):
find ! -iname "abc" -maxdepth 1 -depth -print0 | sed '$d' | xargs -0 rm -r;
备注:
-depth
— 反转输出(因此您不必先删除子目录-maxdepth 1
— 有点无法使用 -深度,但是嘿...这表示仅输出当前目录的内容,而不是子目录(无论如何都会被 -r 选项删除)-print0
和 -0
— 在换行符上分割而不是空格(对于名称中带有空格的目录)sed "$d"
— 表示删除最后一行(因为现在已反转)。最后一行只是一个句点,其中包含将使调用删除目录中的所有内容(和子目录!)我确信有人可以加强这一点,但它确实有效,并且是一个很好的学习机会!
再次感谢 Jared Burrows(以及整个 Unix 社区 — 加油!) — MindWire