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

发布于 2024-07-14 04:59:41 字数 34 浏览 6 评论 0原文

如何删除当前目录(包括当前目录)中的所有文件和子目录?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泪痕残 2024-07-21 04:59:41

在使用 GNU 工具的 bash 下,我会这样做(在大多数情况下应该是安全的):

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

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

TMP=`pwd -P` && cd "`dirname $TMP`" && rm -rf "./`basename $TMP`" && unset TMP

为什么这样更安全:

  • -- 如果我们的目录以破折号开头(非 bash:文件名之前的 ./
  • pwd -P 不仅仅是 pwd 在这种情况下我们不在真正的目录中,而是在指向它的符号链接中。
  • 则参数周围的 "

如果目录包含空格一些随机信息(bash 版本),

  • :末尾的 cd .. 可以省略,但您将处于否则不存在的目录...

编辑:正如 kmkaplan 指出的, -- 东西不是必需的,因为 pwd 返回完整的路径名在 UNIX 上始终以 / 开头

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

我爱人 2024-07-21 04:59:41
olddir=`pwd` && cd .. && rm -rf "$olddir"

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

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

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

老娘不死你永远是小三 2024-07-21 04:59:41
rm -fr "`pwd`"
rm -fr "`pwd`"
缱绻入梦 2024-07-21 04:59:41

我认为这在 DOS / Windows CMD 下是可能的,但我找不到一种在命令之间传输数据的方法。 其他人可能知道解决方法吗?

FOR /F %i IN ('cd') DO SET MyDir=%i | CD .. | RD /S %MyDir%

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?

FOR /F %i IN ('cd') DO SET MyDir=%i | CD .. | RD /S %MyDir%
夜灵血窟げ 2024-07-21 04:59:41

操作系统? 在基于 *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!

勿忘心安 2024-07-21 04:59:41

您只需返回目标文件夹的父文件夹,然后使用“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.

凉栀 2024-07-21 04:59:41

确认后删除当前目录。 如果当前目录是符号链接,则取消链接。

rmd() {
  if [[ -z "$1" ]]; then
    >&2 echo "usage: rmd <dir>"
    return 1
  fi

  dir=$(realpath -se "$1") || return 2

  if [[ -L "$dir" ]]; then
    read -s -k "?Unlink '$dir'? "
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      unlink -- "$dir"
    else
      return 3
    fi
  else
    read -s -k "?Recursively delete '$dir'? "
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      rm -rf -- "$dir"
    else
      return 4
    fi
  fi
}

rm.() {
  rmd "$(pwd -L)" && cd ..
}

用法:

 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.
├── bar -> foo
└── foo

2 directories, 0 files
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ cd bar
 /tmp/tmp.29mUflkHKU/bar                                                                                                             base
❯ rm.
Unlink /tmp/tmp.29mUflkHKU/bar?
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.
└── foo

1 directory, 0 files
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ cd foo
 /tmp/tmp.29mUflkHKU/foo                                                                                                             base
❯ rm.
Recursively delete /tmp/tmp.29mUflkHKU/foo?
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.

0 directories, 0 files

注意:read -s -k 适用于 zsh。 使用 read -n1 -p 进行 bash (源代码)

Delete current directory with confirmation. Unlink if current dir is a symlink.

rmd() {
  if [[ -z "$1" ]]; then
    >&2 echo "usage: rmd <dir>"
    return 1
  fi

  dir=$(realpath -se "$1") || return 2

  if [[ -L "$dir" ]]; then
    read -s -k "?Unlink '$dir'? "
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      unlink -- "$dir"
    else
      return 3
    fi
  else
    read -s -k "?Recursively delete '$dir'? "
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      rm -rf -- "$dir"
    else
      return 4
    fi
  fi
}

rm.() {
  rmd "$(pwd -L)" && cd ..
}

Usage:

 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.
├── bar -> foo
└── foo

2 directories, 0 files
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ cd bar
 /tmp/tmp.29mUflkHKU/bar                                                                                                             base
❯ rm.
Unlink /tmp/tmp.29mUflkHKU/bar?
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.
└── foo

1 directory, 0 files
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ cd foo
 /tmp/tmp.29mUflkHKU/foo                                                                                                             base
❯ rm.
Recursively delete /tmp/tmp.29mUflkHKU/foo?
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.

0 directories, 0 files

Note: read -s -k is for zsh. Use read -n1 -p for bash (source)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文