如何删除当前目录(包括当前目录)中的所有文件?
如何删除当前目录(包括当前目录)中的所有文件和子目录?
How can I delete all files and subdirectories from current directory including current directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在使用 GNU 工具的 bash 下,我会这样做(在大多数情况下应该是安全的):
不在 bash 下并且没有 GNU 工具,我会使用:
为什么这样更安全:
-- 如果我们的目录以破折号开头(非 bash:文件名之前的
./
)pwd -P
不仅仅是pwd
在这种情况下我们不在真正的目录中,而是在指向它的符号链接中。"
如果目录包含空格一些随机信息(bash 版本),
编辑:正如 kmkaplan 指出的,
--
东西不是必需的,因为pwd
返回完整的路径名在 UNIX 上始终以/
开头Under bash with GNU tools, I would do it like that (should be secure in most cases):
not under bash and without GNU tools, I would use:
why this more secure:
--
in cases our directory starts with a dash (non-bash:./
before the filename)pwd -P
not justpwd
in 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 spacessome random info (bash version):
cd ..
at the end can be omitted, but you would be in a non-existant directory otherwise...EDIT: As kmkaplan noted, the
--
thing is not necessary, aspwd
returns the complete path name which always starts with/
on UNIX需要
cd ..
,否则会失败,因为您无法删除当前目录。The
cd ..
is needed, otherwise it will fail since you can't remove the current directory.我认为这在 DOS / Windows CMD 下是可能的,但我找不到一种在命令之间传输数据的方法。 其他人可能知道解决方法吗?
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?
操作系统? 在基于 *NIX 的东西上,您正在寻找“rm -rf directory/”
注意:“递归”的“-r”标志可能很危险!
operating system? on the *NIX-based stuff, you're looking for 'rm -rf directory/'
NOTE: the '-r' flag for 'recursive' can be dangerous!
您只需返回目标文件夹的父文件夹,然后使用“rm -rf yourFolder”即可。
或者您可以使用“rm -rf *”删除当前文件夹中的所有文件和子文件夹。
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.
确认后删除当前目录。 如果当前目录是符号链接,则取消链接。
用法:
注意:
read -s -k
适用于zsh
。 使用read -n1 -p
进行bash
(源代码)Delete current directory with confirmation. Unlink if current dir is a symlink.
Usage:
Note:
read -s -k
is forzsh
. Useread -n1 -p
forbash
(source)