如何删除当前目录中的所有文件,包括当前目录?

时间:2023-01-24 16:48:55

How can I delete all files and subdirectories from current directory including current directory?

如何从当前目录中删除所有文件和子目录,包括当前目录?

6 个解决方案

#1


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)
  • 使用 - 结束参数列表 - 在我们的目录以短划线开头的情况下(非bash:文件名之前的./)

  • pwd -P not just pwd in cases where we are not in a real directory but in a symlink pointing to it.
  • pwd -P不仅仅是在我们不在真实目录中而是在指向它的符号链接中的情况下。

  • "s around the argument in cases the directory contains spaces
  • 在目录包含空格的情况下围绕参数

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 pwd returns the complete path name which always starts with / on UNIX

编辑:正如kmkaplan所指出的那样,事情是没有必要的,因为pwd会返回始终以/在UNIX上开头的完整路径名

#2


olddir=`pwd` && cd .. && rm -rf "$olddir"

The cd .. is needed, otherwise it will fail since you can't remove the current directory.

需要cd ..否则会失败,因为您无法删除当前目录。

#3


rm -fr "`pwd`"

#4


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%

#5


operating system? on the *NIX-based stuff, you're looking for 'rm -rf directory/'

操作系统?在基于* NIX的东西上,你正在寻找'rm -rf目录/'

NOTE: the '-r' flag for 'recursive' can be dangerous!

注意:'递归'的'-r'标志可能很危险!

#6


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 *'从当前文件夹中删除所有文件和子文件夹。

#1


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)
  • 使用 - 结束参数列表 - 在我们的目录以短划线开头的情况下(非bash:文件名之前的./)

  • pwd -P not just pwd in cases where we are not in a real directory but in a symlink pointing to it.
  • pwd -P不仅仅是在我们不在真实目录中而是在指向它的符号链接中的情况下。

  • "s around the argument in cases the directory contains spaces
  • 在目录包含空格的情况下围绕参数

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 pwd returns the complete path name which always starts with / on UNIX

编辑:正如kmkaplan所指出的那样,事情是没有必要的,因为pwd会返回始终以/在UNIX上开头的完整路径名

#2


olddir=`pwd` && cd .. && rm -rf "$olddir"

The cd .. is needed, otherwise it will fail since you can't remove the current directory.

需要cd ..否则会失败,因为您无法删除当前目录。

#3


rm -fr "`pwd`"

#4


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%

#5


operating system? on the *NIX-based stuff, you're looking for 'rm -rf directory/'

操作系统?在基于* NIX的东西上,你正在寻找'rm -rf目录/'

NOTE: the '-r' flag for 'recursive' can be dangerous!

注意:'递归'的'-r'标志可能很危险!

#6


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 *'从当前文件夹中删除所有文件和子文件夹。