在 Unix 中,如何删除当前目录及其下面的所有内容?

发布于 2024-07-18 02:22:41 字数 128 浏览 10 评论 0原文

我知道这将删除子目录及其下面的所有内容:

rm -rf <subdir-name>

但是如何删除当前目录及其下面的每个子目录以及所有这些子目录的内容中的所有内容?

I know this will delete everything in a subdirectory and below it:

rm -rf <subdir-name>

But how do you delete everything in the current directory as well as every subdirectory below it and the contents of all of those subdirectories?

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

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

发布评论

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

评论(10

静谧 2024-07-25 02:22:42

怎么样:

rm -rf "$(pwd -P)"/* 

How about:

rm -rf "$(pwd -P)"/* 
简美 2024-07-25 02:22:42
rm  -rf * 

不要这样做! 这很危险! 确保您位于正确的目录中!

rm  -rf * 

Don't do it! It's dangerous! MAKE SURE YOU'RE IN THE RIGHT DIRECTORY!

我做我的改变 2024-07-25 02:22:42

确保确保您位于正确的目录中

rm -rf *

make sure you are in the correct directory

rm -rf *
你如我软肋 2024-07-25 02:22:42

这个最简单的保险箱& 一般解决方案可能是:

find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf

This simplest safe & general solution is probably:

find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf
筑梦 2024-07-25 02:22:42

我相信这个答案更好:

https://unix .stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory

如果您的顶级目录名为 images,则运行 rm -r images/*。 这使用 shell glob 运算符 * 对图像中的每个文件或目录运行 rm -r

基本上你上一层,然后说删除 X 目录中的所有内容。 这样,您仍然可以指定应删除哪个文件夹的内容,这比仅仅说“删除此处的所有内容”更安全,同时保留原始文件夹(有时您想要这样做,因为您不被允许或只是不想要修改文件夹的现有权限)

I believe this answer is better:

https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory

If your top-level directory is called images, then run rm -r images/*. This uses the shell glob operator * to run rm -r on every file or directory within images.

basically you go up one level, and then say delete everything inside X directory. This way you are still specifying what folder should have its content deleted, which is safer than just saying 'delete everything here", while preserving the original folder, (which sometimes you want to because you aren't allowed or just don't want to modify the folder's existing permissions)

棒棒糖 2024-07-25 02:22:41

实践安全计算。 只需在层次结构中向上一级,并且不使用通配符表达式:

cd ..; rm -rf -- <dir-to-remove>

两个破折号 -- 告诉 rm 是要删除的目录。 不是命令行选项,即使它以破折号开头。

Practice safe computing. Simply go up one level in the hierarchy and don't use a wildcard expression:

cd ..; rm -rf -- <dir-to-remove>

The two dashes -- tell rm that <dir-to-remove> is not a command-line option, even when it begins with a dash.

陌上青苔 2024-07-25 02:22:41

将删除当前文件/目录下的所有文件/目录。

find -mindepth 1 -delete

如果你想对另一个同名的目录做同样的事情,你可以直接命名它。

find <name-of-directory> -mindepth 1 -delete

如果你不仅想删除它的子目录和文件,还想删除目录本身,请省略 -mindepth 1< /代码>。 不使用 -delete 即可获取将要删除的内容的列表。

Will delete all files/directories below the current one.

find -mindepth 1 -delete

If you want to do the same with another directory whose name you have, you can just name that

find <name-of-directory> -mindepth 1 -delete

If you want to remove not only the sub-directories and files of it, but also the directory itself, omit -mindepth 1. Do it without the -delete to get a list of the things that will be removed.

陪你搞怪i 2024-07-25 02:22:41

我总是做的是输入

rm -rf *

然后按 ESC-*,bash 会将 * 扩展为当前工作目录中的文件和目录的显式列表。

好处是:

  • 我可以在按 ENTER 键之前查看要删除的文件列表。
  • 命令历史记录不会包含通配符完好无损的“rm -rf *”,这可能会在错误的时间在错误的位置意外地重复使用。 相反,命令历史记录中将包含实际的文件名。
  • 有一两次回答“等一下......我刚刚删除了哪些文件?”也变得很方便。 文件名在终端回滚缓冲区或命令历史记录中可见。

事实上,我非常喜欢这个,所以我在 .bashrc 中使用这一行将其设置为 TAB 的默认行为:

bind TAB:insert-completions

What I always do is type

rm -rf *

and then hit ESC-*, and bash will expand the * to an explicit list of files and directories in the current working directory.

The benefits are:

  • I can review the list of files to delete before hitting ENTER.
  • The command history will not contain "rm -rf *" with the wildcard intact, which might then be accidentally reused in the wrong place at the wrong time. Instead, the command history will have the actual file names in there.
  • It has also become handy once or twice to answer "wait a second... which files did I just delete?". The file names are visible in the terminal scrollback buffer or the command history.

In fact, I like this so much that I've made it the default behavior for TAB with this line in .bashrc:

bind TAB:insert-completions
倾其所爱 2024-07-25 02:22:41

使用

rm -rf *

更新: . 代表当前目录,但我们不能使用它。 该命令似乎对 ... 进行了显式检查。 请改用通配符通配符。 但这可能有风险。

IMO 更安全的版本是使用:(

rm -ri * 

这会在删除每个文件/目录之前提示您进行确认。)

Use

rm -rf *

Update: The . stands for current directory, but we cannot use this. The command seems to have explicit checks for . and ... Use the wildcard globbing instead. But this can be risky.

A safer version IMO is to use:

rm -ri * 

(this prompts you for confirmation before deleting every file/directory.)

乞讨 2024-07-25 02:22:41

rm –rf . 将直接删除当前目录中的所有内容,包括任何子目录及其内容,这是正确的。 单点 (.) 表示当前目录。 注意不要执行rm -rf ..,因为双点 (..) 表示上一个目录。

话虽这么说,如果您像我一样同时打开多个终端窗口,那么您最好安全地使用 rm -ir 。让我们看看命令参数以了解原因。

首先,如果您查看 rm 命令手册页 (man rm(在大多数 Unix 下)您会注意到 -r 表示“递归地删除目录内容”。 因此,单独执行 rm -r . 将会删除当前目录及其下面的所有内容。

在 rm –rf . 中,添加的 -f 表示“忽略不存在的文件,从不提示”。 该命令会删除当前目录中的所有文件和目录,并且不会提示您确认确实要执行此操作。 如果您在特权用户下运行该命令,-f 特别危险,因为您可能会删除任何目录的内容,而没有机会确保这确实是您想要的。

另一方面,在 rm -ri . 中,替换 -f-i 表示“在任何内容之前提示”移动”。 这意味着在 rm 愉快地删除所有文件之前,您将有机会说“哎呀!这不是我想要的”。

在我早期的系统管理员时代,我在以完全权限(root)登录的情况下在系统上执行了rm -rf /。 结果两天后就从备份中恢复了系统。 这就是我现在使用 rm -ri 的原因。

It is correct that rm –rf . will remove everything in the current directly including any subdirectories and their content. The single dot (.) means the current directory. be carefull not to do rm -rf .. since the double dot (..) means the previous directory.

This being said, if you are like me and have multiple terminal windows open at the same time, you'd better be safe and use rm -ir . Lets look at the command arguments to understand why.

First, if you look at the rm command man page (man rm under most Unix) you notice that –r means "remove the contents of directories recursively". So, doing rm -r . alone would delete everything in the current directory and everything bellow it.

In rm –rf . the added -f means "ignore nonexistent files, never prompt". That command deletes all the files and directories in the current directory and never prompts you to confirm you really want to do that. -f is particularly dangerous if you run the command under a privilege user since you could delete the content of any directory without getting a chance to make sure that's really what you want.

On the otherhand, in rm -ri . the -i that replaces the -f means "prompt before any removal". This means you'll get a chance to say "oups! that's not what I want" before rm goes happily delete all your files.

In my early sysadmin days I did an rm -rf / on a system while logged with full privileges (root). The result was two days passed a restoring the system from backups. That's why I now employ rm -ri now.

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