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 exitBy 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 ./-fooNote that if you use rm to remove a file, it might be possible to recoversome of its contents, given sufficient expertise and/or time. For greaterassurance 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.txtrm: 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.txtrm: remove regular empty file ‘-er.txt’? y[root@localhost tmp]# lslink[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]# lslink[root@localhost tmp]#
可以看到-er.txt已經被刪除了 使用rm *some*命令刪除
有時候,遇到某些帶特殊字元的檔案,使用rm – -filename或者使用rm ./-filename也刪除不了,就得使用rm some命令來刪除,比如要刪除?*&sni.txt這個檔案
這個命令要慎用,*是萬用字元,可能會把帶有some字元的檔案都刪掉
[root@localhost tmp]# lltotal 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] 15211rm: remove regular empty file ‘-er.txt’? bash: sni.txt: command not found...[2]+ Stopped rm -i -- ?*[root@localhost tmp]# rm ./?*&sni.txt[3] 15218rm: remove regular empty file ‘./-er.txt’? bash: sni.txt: command not found...[3]+ Stopped rm -i ./?*[root@localhost tmp]# rm *sni.txtrm: remove regular empty file ‘?*&sni.txt’? y[root@localhost tmp]# lltotal 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]# lltotal 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’? yrm: remove regular empty file ‘testfile’? n[root@localhost tmp]# lltotal 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 -iltotal 074150596 -rw-rw-r--. 1 chenzl chenzl 0 Jan 9 14:27 ?*&sni.txt67132857 -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]#
當使用其中的某種方法行不通時,就把文中的幾種方法都嘗試一下,如果還是不行的話,我就沒轍了‵‵‵‵‵‵‵,不過我相信文中的終極大招能搞定。