bash 如何从当前目录中删除所有文件,包括当前目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/550922/
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
How to delete all files from current directory including current directory?
提问by Ib33X
How can I delete all files and subdirectories from current directory including current directory?
如何从当前目录(包括当前目录)中删除所有文件和子目录?
回答by Johannes Weiss
Under bash with GNU tools, I would do it like that (should be secure in most cases):
在使用 GNU 工具的 bash 下,我会这样做(在大多数情况下应该是安全的):
rm -rf -- "$(pwd -P)" && cd ..
not under bash and without GNU tools, I would use:
不在 bash 下且没有 GNU 工具,我会使用:
TMP=`pwd -P` && cd "`dirname $TMP`" && rm -rf "./`basename $TMP`" && unset TMP
why this more secure:
为什么这更安全:
- end the argument list with
--in cases our directory starts with a dash (non-bash:./before the filename) pwd -Pnot justpwdin cases where we are not in a real directory but in a symlink pointing to it."s around the argument in cases the directory contains spaces
--如果我们的目录以破折号开头(非 bash:./在文件名之前),则以结束参数列表pwd -P不仅仅是pwd在我们不在真实目录中而是在指向它的符号链接中的情况下。"在目录包含空格的情况下围绕参数
some random info (bash version):
一些随机信息(bash 版本):
- the
cd ..at the end can be omitted, but you would be in a non-existant directory otherwise...
- 将
cd ..在年底可以省略,但你会在一个不存在的目录,否则......
EDIT: As kmkaplan noted, the --thing is not necessary, as pwdreturns the complete path name which always starts with /on UNIX
编辑:正如kmkaplan所指出的,这--不是必需的,因为pwd返回/在UNIX上始终以开头的完整路径名
回答by dwc
olddir=`pwd` && cd .. && rm -rf "$olddir"
The cd ..is needed, otherwise it will fail since you can't remove the current directory.
该cd ..是需要的,否则会失败,因为你不能删除当前目录。
回答by kmkaplan
rm -fr "`pwd`"
回答by Kristen
I think this would be possible under DOS / Windows CMD, but I can't quite find a way to pipe the data between commands. Someone else may know the fix for that?
我认为这在 DOS / Windows CMD 下是可能的,但我找不到一种方法来在命令之间传输数据。其他人可能知道解决方法?
FOR /F %i IN ('cd') DO SET MyDir=%i | CD .. | RD /S %MyDir%
回答by kext
You just can go back the target folder's parent folder, then use 'rm -rf yourFolder'. or you can use 'rm -rf *' to delete all files and subfolders from the current folder.
您只需返回目标文件夹的父文件夹,然后使用“rm -rf yourFolder”即可。或者您可以使用 'rm -rf *' 从当前文件夹中删除所有文件和子文件夹。
回答by neoice
operating system? on the *NIX-based stuff, you're looking for 'rm -rf directory/'
操作系统?在基于 *NIX 的东西上,您正在寻找 'rm -rf directory/'
NOTE: the '-r' flag for 'recursive' can be dangerous!
注意:“递归”的“-r”标志可能很危险!

