码迷,mamicode.com
首页 > 系统相关 > 详细

Linux如何删除以特殊字符开头的文件

时间:2014-11-19 01:17:31      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

  作者:zhanhailiang 日期:2014-11-18

Linux在使用一些工具时经常会生成一些以特殊字符开头的文件,如以-开头,此时若使用普通rm,会提示“invalid option”:

[root@~]# rm -\,trail\:- 
rm: invalid option -- ‘,‘
Try `rm ./-,trail:-‘ to remove the file `-,trail:-‘.
Try `rm --help‘ for more information.

这是因为Linux Shell默认认为-o|–option表示选项配置,而在当前命令rm中读不到相应的选项配置,所以报错。

通过rm –help可以看到:

To remove a file whose name starts with a `-‘, for example `-foo‘,
use one of these commands:
  rm -- -foo
 
  rm ./-foo

总之,要删除掉特殊文件,可以使用绝对路径或相对路径或–来避免错误地将文件名解析为选项配置,以下提供多种操作方案:

第一种:rm – filename

[root@~]# rm -- -\,trail\:- 
rm: remove regular empty file `-,trail:-‘? 

第二种:rm 相对路径

[root@~]# rm ./-\,trail\:- 
rm: remove regular empty file `./-,trail:-‘? 

第三种:rm 绝对路径

[root@~]# rm ~/-\,trail\:- 
rm: remove regular empty file `/root/-,trail:-‘?

更深入一点,分析源码:

首先,下载源码包:

[root@~/software]# wget http://down1.chinaunix.net/distfiles/coreutils-8.4.tar.gz

查看src/rm.c源码:

bubuko.com,布布扣

可见,当参数以特殊字符-开头,且不在选项配置中时rm直接报错。

Linux如何删除以特殊字符开头的文件

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/billfeller/article/details/41255543

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!