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

linux 删除含特殊字符文件

2013-10-12 11:35 477 查看
http://blog.csdn.net/hittata/article/details/8020156

rm 语法

[cpp]
view plaincopy

[root@andes.com ~]#rm --help
Usage: rm [OPTION]... FILE...
Remove (unlink) the FILE(s).

-f, --force ignore nonexistent files, 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
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit

特殊文件产生的原因 :误操作,以及一些编码问题导致文件名比较特殊,包含特殊字符,或无法打出的特殊字符,需要删除,可以参照如下几点:
1.文件名以 - 开头 , -filename

直接 rm -filename 报错误选项。

原因,-在rm 中是option 开头字段

解决办法: rm -- -filename 或者 rm ./-filename

[cpp]
view plaincopy

[root@andes.com ~/tmp/dir]#> -ta
[root@andes.com ~/tmp/dir]#ls
-ta
[root@andes.com ~/tmp/dir]#rm -ta
rm: invalid option -- 't'
Try `rm ./-ta' to remove the file `-ta'.
Try `rm --help' for more information.
[root@andes.com ~/tmp/dir]#rm ./-ta
rm: remove regular empty file `./-ta'? y
[root@andes.com ~/tmp/dir]#

2.文件名以其他特殊字符开头,可以考虑通配符删除

可以使用通配符 rm *some* 之类进行删除

3.文件名是无法打印的乱码字符

进入要删除文件目录, rm -i * 逐个由系统提示是否删除,遇到想删除文件就Y,保留就N,较繁琐,

如遇到普通正常文件特别多可以考虑将其移动到其他目录:

find . -name "[a-z|A-Z]*" |xargs -I {} mv {} /somepath

个人认为第三种方法无敌,没有删不掉的文件,只有不努力的程序员。

4.通过inode节点找到文件删除

ls -i

find -inum XXX |xargs -I {} rm {}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: