rm + 如何删除带有特殊字符的文件

yae*_*ael 3 linux ksh rm

我的 linux 机器中有这个文件:

 ----------9976723563nneh4_-----192.9.200.4
Run Code Online (Sandbox Code Playgroud)

我尝试删除这个文件,但我不能在这里看到:

需要向 rm 添加什么才能删除此文件?

rm "----------9976723563nneh4_-----192.9.200.4"
rm: illegal option -- --------9976723563nneh4_-----192.9.200.4
usage: rm [-fiRr] file ...
Run Code Online (Sandbox Code Playgroud)

.

rm '----------9976723563nneh4_-----192.9.200.4'
rm: illegal option -- --------9976723563nneh4_-----192.9.200.4
usage: rm [-fiRr] file ...
Run Code Online (Sandbox Code Playgroud)

Hau*_*ing 8

rm -- ----------9976723563nneh4_-----192.9.200.4
Run Code Online (Sandbox Code Playgroud)

您需要--告诉rm(以及或多或少所有其他 GNU 软件)以下所有参数都是文件名,即使以“-”开头也是如此。否则(在您的情况下)文件名会与选项混淆。另一种可能是

rm ./----------9976723563nneh4_-----192.9.200.4
Run Code Online (Sandbox Code Playgroud)

编辑 1

将这个微不足道的答案称为“很好的解决方案”让我觉得有必要将其提升到更高的水平。基本问题无法“更好地”解决,但您可以尝试习惯于始终进行--争论。一些 shell 代码(用 shell 函数替换 rm)可以帮助你解决这个问题:

rm () {
  local args sep_found=no
  args=("$@")
  while [ $# -gt 0 ]; do
    if [ "--" = "$1" ]; then
      sep_found=yes
      break
    fi
    shift
  done
  if [ "yes" = "$sep_found" ]; then
    command rm "${args[@]}"
  else
    echo "WARNING: rm called without '--' seperator."
    echo "Please correct the command; aborting."
  fi
}
Run Code Online (Sandbox Code Playgroud)

你会把它放在 eg 中~/.bashrc