首页 > 系统服务 > 详细

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

时间:2014-11-19 01:16:01      阅读:173      评论:0      收藏:0      [点我收藏+]
  作者: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如何删除以特殊字符开头的文件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!