如何通过终端(bash shell)快速删除文件和目录

发布于 2024-08-29 03:01:53 字数 1549 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水染的天色ゝ 2024-09-05 03:01:53
rm -rf some_dir

-r“递归”
-f“force”(禁止确认消息)

小心!

rm -rf some_dir

-r "recursive"
-f "force" (suppress confirmation messages)

Be careful!

音盲 2024-09-05 03:01:53
rm -rf *

将删除当前目录中的所有内容(文件夹和文件)。

但要小心! 仅当您完全确定您位于正确的目录中时才执行此命令。

rm -rf *

Would remove everything (folders & files) in the current directory.

But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.

眼眸里的快感 2024-09-05 03:01:53

是的,有。 -r 选项告诉 rmr 递归的,并删除以其参数为根的整个文件层次结构;换句话说,如果给定一个目录,它将删除其所有内容,然后执行有效的 rmdir 操作。

您应该知道的另外两个选项是 -i-f-i 代表i互动;它会让 rm 在删除每个文件之前提示您。 -f代表force;它会继续删除所有内容而不询问。 -i 更安全,但 -f 更快;仅当您绝对确定您正在删除正确的内容时才使用它。您可以使用 -r 指定这些,也可以不指定;这是一个独立的设置。

和往常一样,您可以组合开关:rm -r -i 只是 rm -rirm -r -f>rm -rf

另请注意,您正在学习的内容适用于每个 Unix 操作系统上的 bash:OS X、Linux、FreeBSD 等。事实上,rm 的语法与几乎每个 Unix 操作系统上的每个 shell。 OS X 在本质上是一个 BSD Unix 系统。

Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir.

The other two options you should know are -i and -f. -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you're absolutely sure you're deleting the right thing. You can specify these with -r or not; it's an independent setting.

And as usual, you can combine switches: rm -r -i is just rm -ri, and rm -r -f is rm -rf.

Also note that what you're learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm's syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.

情定在深秋 2024-09-05 03:01:53

我正在寻找一种方法来删除目录中的所有文件,除了我想保留的一些目录和文件。我设计了一种使用 find 来做到这一点的方法:

find -E . -regex './(dir1|dir2|dir3)' -and -type d -prune -o -print -exec rm -rf {} \;

本质上,它使用正则表达式来选择要从结果中排除的目录,然后删除剩余的文件。

I was looking for a way to remove all files in a directory except for some directories, and files, I wanted to keep around. I devised a way to do it using find:

find -E . -regex './(dir1|dir2|dir3)' -and -type d -prune -o -print -exec rm -rf {} \;

Essentially it uses regex to select the directories to exclude from the results then removes the remaining files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文