CentOS 删除特殊文件名文件

rm.jpg[1]

CentOS 删除特殊文件名文件

在CentOS 下我们一般使用 rm 命令删除文件。

CentOS 命令一般使用 -后面跟字母来作为命令的选项,如果我们要删除的文件名以 - 开头该怎么处理呢?

我们通过查看rm的帮助可以找到答案:

[root@host]# rm --help
Usage: rm [OPTION]... FILE...
Remove (unlink) the FILE(s).

  -f, --force           ignore nonexistent files, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively.  Less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i).  Without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat `/' specially
      --preserve-root   do not remove `/' (default)
  -r, -R, --recursive   remove directories and their contents recursively
  -v, --verbose         explain what is being done
      --help     display this help and exit
      --version  output version information and exit

By default, rm does not remove directories.  Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.

To remove a file whose name starts with a `-', for example `-foo',
use one of these commands:
  rm -- -foo

  rm ./-foo

Note that if you use rm to remove a file, it is usually possible to recover
the contents of that file.  If you want more assurance that the contents are
truly unrecoverable, consider using shred.

Report rm bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'rm invocation'

我们可以看到有两种方法可以删除以-开头的文件名的文件:

To remove a file whose name starts with a `-', for example `-foo',
use one of these commands:
  rm -- -foo

  rm ./-foo

第一种:在文件名前使用 -- 解决文件名被解读为命令选项的歧义,比如删除-foo文件 rm -f -- -foo
第二种:利用相对路径让文件名称变为不是以 - 开头,比如删除当前目录的-foo文件 rm -f ./-foo
Bye!!!

添加新评论