今天遇到一件比較神奇的事,由於之前用下列命令安裝基於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命令有時候還是蠻智能的,我們在日常的操作中,要多留心它給出的提示與建議,然後多動手試,這樣的話,收穫就很大。