在 Unix 中,如何删除当前目录及其下面的所有内容?
我知道这将删除子目录及其下面的所有内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
怎么样:
How about:
不要这样做! 这很危险! 确保您位于正确的目录中!
Don't do it! It's dangerous! MAKE SURE YOU'RE IN THE RIGHT DIRECTORY!
确保确保您位于正确的目录中
make sure you are in the correct directory
这个最简单的保险箱& 一般解决方案可能是:
This simplest safe & general solution is probably:
我相信这个答案更好:
https://unix .stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory
基本上你上一层,然后说删除 X 目录中的所有内容。 这样,您仍然可以指定应删除哪个文件夹的内容,这比仅仅说“删除此处的所有内容”更安全,同时保留原始文件夹(有时您想要这样做,因为您不被允许或只是不想要修改文件夹的现有权限)
I believe this answer is better:
https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory
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)
实践安全计算。 只需在层次结构中向上一级,并且不使用通配符表达式:
两个破折号
--
告诉rm
是要删除的目录。
不是命令行选项,即使它以破折号开头。Practice safe computing. Simply go up one level in the hierarchy and don't use a wildcard expression:
The two dashes
--
tellrm
that<dir-to-remove>
is not a command-line option, even when it begins with a dash.将删除当前文件/目录下的所有文件/目录。
如果你想对另一个同名的目录做同样的事情,你可以直接命名它。
如果你不仅想删除它的子目录和文件,还想删除目录本身,请省略
-mindepth 1< /代码>。 不使用
-delete
即可获取将要删除的内容的列表。Will delete all files/directories below the current one.
If you want to do the same with another directory whose name you have, you can just name that
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.我总是做的是输入
并然后按 ESC-*,bash 会将 * 扩展为当前工作目录中的文件和目录的显式列表。
好处是:
事实上,我非常喜欢这个,所以我在 .bashrc 中使用这一行将其设置为 TAB 的默认行为:
What I always do is type
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:
In fact, I like this so much that I've made it the default behavior for TAB with this line in .bashrc:
使用
更新:
.
代表当前目录,但我们不能使用它。 该命令似乎对.
和..
进行了显式检查。 请改用通配符通配符。 但这可能有风险。IMO 更安全的版本是使用:(
这会在删除每个文件/目录之前提示您进行确认。)
Use
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:
(this prompts you for confirmation before deleting every file/directory.)
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 dorm -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, doingrm -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 employrm -ri
now.