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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/820760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:09:42  来源:igfitidea点击:

In Unix, how do you remove everything in the current directory and below it?

bashunixdirectorysubdirectorydelete-directory

提问by Yen

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?

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

回答by tvanfosson

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 rmthat <dir-to-remove>is not a command-line option, even when it begins with a dash.

两个破折号--告诉rm<dir-to-remove>不是一个命令行选项,即使它与破折号开始。

回答by Johannes Schaub - litb

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 -deleteto get a list of the things that will be removed.

如果不仅要删除它的子目录和文件,还要删除目录本身,请省略-mindepth 1. 在没有 的情况下执行此操作-delete以获取将被删除的内容的列表。

回答by Ville Laurikari

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.

然后打ESC- *,和bash将扩大*在当前工作目录中的文件和目录的显式列表。

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.
  • 我可以在按 ENTER 之前查看要删除的文件列表。
  • 命令历史将不包含带有完整通配符的“rm -rf *”,然后可能会在错误的时间在错误的地方意外重用。相反,命令历史记录中将包含实际的文件名。
  • 回答“等一下......我刚刚删除了哪些文件?”也变得很方便。文件名在终端回滚缓冲区或命令历史记录中可见。

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

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

bind TAB:insert-completions

回答by dirkgently

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:

更安全的 IMO 版本是使用:

rm -ri * 

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

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

回答by dirkgently

How about:

怎么样:

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

回答by Pierre-Luc Simard

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.

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

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.

话虽如此,如果你像我一样同时打开多个终端窗口,你最好是安全的,并使用rm -ir .让我们看看命令参数来了解原因。

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

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

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. -fis 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.

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

On the otherhand, in rm -ri .the -ithat replaces the -fmeans "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.

在otherhand,在rm -ri .-i替换的-f意思是“任何拆除前提示”。这意味着在 rm 愉快地删除所有文件之前,您将有机会说“哎呀!这不是我想要的”。

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 -rinow.

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

回答by nkassis

rm  -rf * 

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

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

回答by digitaljoel

make sureyou are in the correct directory

使确定你是在正确的目录

rm -rf *

回答by mark

This simplest safe & general solution is probably:

这个最简单的安全和通用解决方案可能是:

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

回答by Francisco Noriega

I believe this answer is better:

我相信这个答案更好:

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

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 -ron every file or directory within images.

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

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)

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