您的位置:首页 > 运维架构 > Linux

基于CentOS7开发之路 --- 日常使用 :Linux 删除带有特殊字符的文件

2017-01-09 14:17 866 查看

Linux 删除带有特殊字符的文件

有时候我们需要删除一些带有特殊字符的文件,然而却遇到不懂如何删除的情况,比如我就是这样,下面是我已了解的几种删除方法:

rm – -filename

rm ./-filename

rm *some*

rm -i *

通过inode号删除

了解rm语法

首先了解以下rm的语法,其实在此之前我也没有好好了解过‵‵‵‵‵,在终端窗口里面键入rm –help

[root@localhost tmp]$ rm --help
Usage: rm [OPTION]... FILE...
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/>
For complete documentation, run: info coreutils 'rm invocation'


使用rm – -filename命令删除

rm直接删除文件名开头为 - 的文件,会提示失败

[root@localhost tmp]# rm -er.txt
rm: invalid option -- 'e'
Try 'rm ./-er.txt' to remove the file ‘-er.txt’.
Try 'rm --help' for more information.
[root@localhost tmp]#


使用rm – -filename删除,注意,rm后面是两个-

[root@localhost tmp]# rm -- -er.txt
rm: remove regular empty file ‘-er.txt’? y
[root@localhost tmp]# ls
link
[root@localhost tmp]#
[root@localhost tmp]#


可以看到-er.txt已经被删除了

使用rm ./-filename命令删除

在使用rm – -filename命令删除文件时,已经提示可以尝试使用rm ./-er.txt去删除文件

[root@localhost tmp]# rm ./-er.txt
rm: remove regular empty file ‘./-er.txt’? y
[root@localhost tmp]# ls
link
[root@localhost tmp]#


可以看到-er.txt已经被删除了

使用rm *some*命令删除

有时候,遇到某些带特殊字符的文件,使用rm – -filename或者使用rm ./-filename也删除不了,就得使用rm some命令来删除,比如要删除?*&sni.txt这个文件

这个命令要慎用,*是通配符,可能会把带有some字符的文件都删掉

[root@localhost tmp]# ll
total 0
-rw-rw-r--. 1 root   root   0 Jan  9 12:10 -er.txt
-rw-rw-r--. 1 root   root   0 Jan  9 12:10 -rr20170109122610.txt
-rw-rw-r--. 1 chenzl chenzl 0 Jan  9 14:05 ?*&sni.txt
[root@localhost tmp]# rm -- ?*&sni.txt
[2] 15211
rm: remove regular empty file ‘-er.txt’? bash: sni.txt: command not found...

[2]+  Stopped                 rm -i -- ?*
[root@localhost tmp]# rm ./?*&sni.txt
[3] 15218
rm: remove regular empty file ‘./-er.txt’? bash: sni.txt: command not found...

[3]+  Stopped                 rm -i ./?*
[root@localhost tmp]# rm *sni.txt
rm: remove regular empty file ‘?*&sni.txt’? y
[root@localhost tmp]# ll
total 0
-rw-rw-r--. 1 root root 0 Jan  9 12:10 -er.txt
-rw-rw-r--. 1 root root 0 Jan  9 12:10 -rr20170109122610.txt
[root@localhost tmp]#


使用rm -i *命令删除

如果使用上面几个方法还是不能删除,可以尝试一下终极大招,比如我想删除?*&sni.txt这个文件,当提示是想要删除的文件的时候,输入y,否则输入n

[root@localhost tmp]# ll
total 0
-rw-rw-r--. 1 chenzl chenzl 0 Jan  9 14:14 ?*&sni.txt
-rw-r--r--. 1 root   root   0 Jan  9 14:15 testfile
[root@localhost tmp]# rm -i *
rm: remove regular empty file ‘?*&sni.txt’? y
rm: remove regular empty file ‘testfile’? n
[root@localhost tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Jan  9 14:15 testfile
[root@localhost tmp]#


通过inode号删除

还可以通过文件的inode号来删除,首先找出文件的inode号,使用ls -il即可看到,比如?*&sni.txt这个文件的inode号为74150596,再结合find命令删除文件。

[root@localhost tmp]# ls -il
total 0
74150596 -rw-rw-r--. 1 chenzl chenzl 0 Jan  9 14:27 ?*&sni.txt
67132857 -rw-r--r--. 1 root   root   0 Jan  9 14:15 testfile
[root@localhost tmp]# find . -inum 74150596 -exec rm -i {} \;
rm: remove regular empty file ‘./?*&sni.txt’? y
[root@localhost tmp]#


当使用其中的某种方法行不通时,就把文中的几种方法都尝试一下,如果还是不行的话,我就没辙了‵‵‵‵‵‵‵,不过我相信文中的终极大招能搞定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: