在Linux中如何删除以--开头的文件

今天遇到一件比较神奇的事,由于之前用下列命令安装基于docker的nginx的时候,少写了换行转接符’’,所以导致用户目录多了一个名为–public的文件。

正确的docker命令

docker run --detach \
--restart always \
--name nginx \
--publish 80:80 \
--publish 443:443 \
--volume /data/docker/nginx/etc:/etc/nginx \
--volume /data/docker/nginx/data:/usr/share/nginx \
nginx:1.17.9

缺失换行转接符’'的命令

docker run --detach 
--restart always 
--name nginx 
--publish 80:80 
--publish 443:443 
--volume /data/docker/nginx/etc:/etc/nginx 
--volume /data/docker/nginx/data:/usr/share/nginx 
nginx:1.17.9

用户目录现状

[root@xtwj73 ~]# ll
total 60
dr-xr-x---. 10 root root  4096 Jun 24 09:44 .
dr-xr-xr-x. 19 root root   248 Mar 19 15:04 ..
-rw-------.  1 root root  1710 Dec  2  2019 anaconda-ks.cfg
-rw-------.  1 root root  8769 Jun 24 10:00 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root   210 May  9 14:28 .bash_profile
-rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
drwx------.  3 root root    25 Mar 10 13:26 .config
-rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
drwxr-xr-x.  2 root root   218 Mar 19 15:08 .dubbo
-rw-------.  1 root root    53 Mar 18 16:12 .lesshst
drwxr-xr-x.  3 root root    19 Mar 19 15:08 logs
drwxr-xr-x.  4 root root    34 May 19 15:32 nacos
drwxr-xr-x.  5 root root    83 Mar 10 16:59 .npm
-rw-r--r--.  1 root root    42 Mar 10 13:26 .npmrc
drwxr-----.  3 root root    19 Dec  2  2019 .pki
-rw-r--r--.  1 root root     0 Mar 11 09:52 --publish
drwx------.  2 root root    80 May 19 10:15 .ssh
-rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc
drwxr-xr-x.  2 root root    31 May 27 15:07 .tmp
-rw-------.  1 root root 11211 Jun 24 09:44 .viminfo
[root@xtwj73 ~]#

我们知道文件~/–publish是一个垃圾文件,于是我打算将其删除,下面是删除操作:

[root@xtwj73 ~]# rm -rf "--publish"
rm: unrecognized option '--publish'
Try 'rm ./--publish' to remove the file ‘--publish’.
Try 'rm --help' for more information.
[root@xtwj73 ~]# 

这个时候,我们发现删除不了,初步怀疑是文件名太特殊了,那有没有办法解决呢,于是我想到man,接下来,我们查看一下rm的man信息,看看有没有好的解决方案。

[root@xtwj73 ~]# man rm |cat
RM(1)                                                                                                         User Commands                                                                                                        RM(1)



NAME
       rm - remove files or directories

SYNOPSIS
       rm [OPTION]... FILE...

DESCRIPTION
       This manual page documents the GNU version of rm.  rm removes each specified file.  By default, it does not remove directories.

       If  the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then rm prompts the user for whether to proceed with the entire operation.  If the response is not
       affirmative, the entire command is aborted.

       Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether  to  remove  the  file.   If  the
       response is not affirmative, the file is skipped.

OPTIONS
       Remove (unlink) the FILE(s).

       -f, --force
              ignore nonexistent files and arguments, 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

       -d, --dir
              remove empty directories

       -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 might be possible to recover some of its contents, given sufficient expertise and/or time.  For greater assurance that the contents are truly unrecoverable, consider using shred.

       GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report rm translation bugs to <http://translationproject.org/team/>

AUTHOR
       Written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.

COPYRIGHT
       Copyright © 2013 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       unlink(1), unlink(2), chattr(1), shred(1)

       The full documentation for rm is maintained as a Texinfo manual.  If the info and rm programs are properly installed at your site, the command

              info coreutils 'rm invocation'

       should give you access to the complete manual.



GNU coreutils 8.22                                                                                             August 2019                                                                                                         RM(1)
[root@xtwj73 ~]# 

我们看到第59~63行提到,可以用参数-代表匹配带一个以-开头的文件,那么能不能用–匹配以–开头的文件呢,我们不妨试一下。

[root@xtwj73 ~]# rm -- --publish
rm: remove regular empty file ‘--publish’? y
[root@xtwj73 ~]# ll
total 56
dr-xr-x---. 10 root root   283 Jun 24 12:29 .
dr-xr-xr-x. 19 root root   248 Mar 19 15:04 ..
-rw-------.  1 root root  1710 Dec  2  2019 anaconda-ks.cfg
-rw-------.  1 root root  8769 Jun 24 10:00 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root   210 May  9 14:28 .bash_profile
-rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
drwx------.  3 root root    25 Mar 10 13:26 .config
-rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
drwxr-xr-x.  2 root root   218 Mar 19 15:08 .dubbo
-rw-------.  1 root root    53 Mar 18 16:12 .lesshst
drwxr-xr-x.  3 root root    19 Mar 19 15:08 logs
drwxr-xr-x.  4 root root    34 May 19 15:32 nacos
drwxr-xr-x.  5 root root    83 Mar 10 16:59 .npm
-rw-r--r--.  1 root root    42 Mar 10 13:26 .npmrc
drwxr-----.  3 root root    19 Dec  2  2019 .pki
drwx------.  2 root root    80 May 19 10:15 .ssh
-rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc
drwxr-xr-x.  2 root root    31 May 27 15:07 .tmp
-rw-------.  1 root root 11211 Jun 24 09:44 .viminfo
[root@xtwj73 ~]# 

实践证明,这样是可以的。

那么还有没别的办法可以删除这样的文件呢?

我们还记得第一次删除–publish文件的时候,rm给出的提示吗?

Try 'rm ./--publish' to remove the file ‘--publish’.

rm推荐我们用命令’rm ./–publish’试试,那我们现在用这个命令试一下。为了重现实验现场,我们重新生成一个名称为–publish的文件。

[root@xtwj73 ~]# echo '' > --publish
[root@xtwj73 ~]# ll
total 64
dr-xr-x---. 10 root root  4096 Jun 24 13:03 .
dr-xr-xr-x. 19 root root   248 Mar 19 15:04 ..
-rw-------.  1 root root  1710 Dec  2  2019 anaconda-ks.cfg
-rw-------.  1 root root  8769 Jun 24 10:00 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root   210 May  9 14:28 .bash_profile
-rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
drwx------.  3 root root    25 Mar 10 13:26 .config
-rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
drwxr-xr-x.  2 root root   218 Mar 19 15:08 .dubbo
-rw-------.  1 root root    53 Mar 18 16:12 .lesshst
drwxr-xr-x.  3 root root    19 Mar 19 15:08 logs
drwxr-xr-x.  4 root root    34 May 19 15:32 nacos
drwxr-xr-x.  5 root root    83 Mar 10 16:59 .npm
-rw-r--r--.  1 root root    42 Mar 10 13:26 .npmrc
drwxr-----.  3 root root    19 Dec  2  2019 .pki
-rw-r--r--.  1 root root     1 Jun 24 13:03 --publish
drwx------.  2 root root    80 May 19 10:15 .ssh
-rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc
drwxr-xr-x.  2 root root    31 May 27 15:07 .tmp
-rw-------.  1 root root 11211 Jun 24 09:44 .viminfo
[root@xtwj73 ~]# rm -rf ./--publish 
[root@xtwj73 ~]# ll
total 56
dr-xr-x---. 10 root root   283 Jun 24 13:03 .
dr-xr-xr-x. 19 root root   248 Mar 19 15:04 ..
-rw-------.  1 root root  1710 Dec  2  2019 anaconda-ks.cfg
-rw-------.  1 root root  8769 Jun 24 10:00 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root   210 May  9 14:28 .bash_profile
-rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
drwx------.  3 root root    25 Mar 10 13:26 .config
-rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
drwxr-xr-x.  2 root root   218 Mar 19 15:08 .dubbo
-rw-------.  1 root root    53 Mar 18 16:12 .lesshst
drwxr-xr-x.  3 root root    19 Mar 19 15:08 logs
drwxr-xr-x.  4 root root    34 May 19 15:32 nacos
drwxr-xr-x.  5 root root    83 Mar 10 16:59 .npm
-rw-r--r--.  1 root root    42 Mar 10 13:26 .npmrc
drwxr-----.  3 root root    19 Dec  2  2019 .pki
drwx------.  2 root root    80 May 19 10:15 .ssh
-rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc
drwxr-xr-x.  2 root root    31 May 27 15:07 .tmp
-rw-------.  1 root root 11211 Jun 24 09:44 .viminfo
[root@xtwj73 ~]# 

我们发现也是可以的。由此可见,Linux命令有时候还是蛮智能的,我们在日常的操作中,要多留心它给出的提示与建议,然后多动手试,这样的话,收获就很大。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章