Rumah  >  Soal Jawab  >  teks badan

linux运维 - linux下 怎样删除文件名中包含特殊字符的文件

目录中无意间出现了 -- 这个文件

[root@dev tmp]# ls
--  00  01  02  03  04  05  06  07  08  09

[root@dev tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 23 15:31 --
-rw-r--r-- 1 root root 0 Oct 23 15:37 00
-rw-r--r-- 1 root root 0 Oct 23 15:37 01
-rw-r--r-- 1 root root 0 Oct 23 15:37 02
-rw-r--r-- 1 root root 0 Oct 23 15:37 03
-rw-r--r-- 1 root root 0 Oct 23 15:37 04
-rw-r--r-- 1 root root 0 Oct 23 15:37 05
-rw-r--r-- 1 root root 0 Oct 23 15:37 06
-rw-r--r-- 1 root root 0 Oct 23 15:37 07
-rw-r--r-- 1 root root 0 Oct 23 15:37 08
-rw-r--r-- 1 root root 0 Oct 23 15:37 09

怎么删都无法删除

[root@dev tmp]# rm -rf * 
[root@dev tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 23 15:31 --

甚至是rm -rf

[root@dev tmp]# rm -rf \-\- && ll
total 0
-rw-r--r-- 1 root root 0 Oct 23 15:31 --
[root@dev tmp]# rm -rf \\-\\- && ll
total 0
-rw-r--r-- 1 root root 0 Oct 23 15:31 --
[root@dev tmp]# mv --  xx
mv: missing destination file operand after `xx'
Try `mv --help' for more information.
[root@dev tmp]# mv \-\- xx
mv: missing destination file operand after `xx'
Try `mv --help' for more information.

为什么这样的字符无法删掉、rm命令是怎么处理的?

PHP中文网PHP中文网2742 hari yang lalu767

membalas semua(2)saya akan balas

  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:41:48

    可以看下手册

    [root@cc tmp]# rm --help
    
    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.
    

    所以有两种方法可以删除

    加上 -- 或者 ./ 就可以删除了

    [root@cc tmp]# rm -rf -- --
    [root@cc tmp]# rm -rf ./--

    balas
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:41:48

    rm -rf -- --

    --对于大部分命令来说是用来标示命令自身参数的结束

    比如

    $ echo a > test
    $ cat -n test
         1    a
    $ cat -n -- test
         1    a
    $ cat -- -n test
    cat: -n: 没有那个文件或目录
    a

    -- 之前是命令自身的配置参数, 之后是命令将要作用的文件、对象等参数

    当你只写一个-- 就被当做分隔符了, 写两次, 第一个被当做分隔符,第二个就被当做命令参数了

    balas
    0
  • Batalbalas