使用查找-删除所有文件/目录(在Linux中),除任何一个以外 [英] Using find - Deleting all files/directories (in Linux ) except any one

查看:342
本文介绍了使用查找-删除所有文件/目录(在Linux中),除任何一个以外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要删除我们使用的所有文件和目录,请rm -rf *.

If we want to delete all files and directories we use, rm -rf *.

但是,如果我想一次删除所有文件和目录,除了一个特定文件,该怎么办?

But what if i want all files and directories be deleted at a shot, except one particular file?

有什么命令吗? rm -rf *可以轻松删除,但可以删除我最喜欢的文件/目录.

Is there any command for that? rm -rf * gives the ease of deletion at one shot, but deletes even my favourite file/directory.

预先感谢

推荐答案

find 可以成为一个很好的朋友:

find can be a very good friend:

$ ls
a/  b/  c/
$ find * -maxdepth 0 -name 'b' -prune -o -exec rm -rf '{}' ';'
$ ls
b/
$ 

说明:

  • find * -maxdepth 0:选择*选择的所有内容,而无需进入任何目录

  • find * -maxdepth 0: select everything selected by * without descending into any directories

-name 'b' -prune:请勿打扰(-prune)与条件-name 'b'

-name 'b' -prune: do not bother (-prune) with anything that matches the condition -name 'b'

-o -exec rm -rf '{}' ';':调用rm -rf进行其他操作

另外,另一种可能更简单的方法是移动或重命名您的收藏夹目录,以免受到干扰:

By the way, another, possibly simpler, way would be to move or rename your favourite directory so that it is not in the way:

$ ls
a/  b/  c/
$ mv b .b
$ ls
a/  c/
$ rm -rf *
$ mv .b b
$ ls
b/

这篇关于使用查找-删除所有文件/目录(在Linux中),除任何一个以外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆