如何从当前目录删除所有文件

如何从当前目录删除所有文件

本文介绍了如何从当前目录删除所有文件,包括当前目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

(在大多数情况下应该是安全的):

  rm -rf  - $(pwd -P) && cd .. 

不在bash下,没有GNU工具,我会使用:

  TMP =`pwd -P`&& cd`dirname $ TMP`&&& rm -rf./'basename $ TMP`&&&取消设置TMP 

为什么这样更安全:



    ./ >
  • / code>之前的文件名)

  • pwd -P 不只是 pwd 在我们不在一个真实的目录,但在一个符号链接指向它。

  • (bash版本):



    ul>
  • 结尾处的 cd .. 可以省略,但您将位于不存在的目录中,否则...



EDIT :如kmkaplan指出的, - 不必要,因为 pwd 返回在UNIX上始终以 / 开头的完整路径名


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

解决方案

Under bash with GNU tools, I would do it like that (should be secure in most cases):

rm -rf -- "$(pwd -P)" && cd ..

not under bash and without GNU tools, I would use:

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 -P not just pwd 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 spaces

some random info (bash version):

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

这篇关于如何从当前目录删除所有文件,包括当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:22