linux 删除包含字符串,linux删除含特殊字符的文件或文件夹

linux系统支持用户创建包含连字符(-)的文件或文件夹,一个错误的操作有时可能会产生以下文件或文件夹:

[root@huntdb aa]# touch /tmp/aa/{,-,\\}-{0,1,2,3,4,5}

[root@huntdb aa]# ls

-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  -5  --5  \-5

[root@huntdb aa]# ll

total 0

-rw-r--r-- 1 root root 0 Apr  2 22:52 -0

-rw-r--r-- 1 root root 0 Apr  2 22:52 --0

-rw-r--r-- 1 root root 0 Apr  2 22:52 \-0

-rw-r--r-- 1 root root 0 Apr  2 22:52 -1

-rw-r--r-- 1 root root 0 Apr  2 22:52 --1

-rw-r--r-- 1 root root 0 Apr  2 22:52 \-1

-rw-r--r-- 1 root root 0 Apr  2 22:52 -2

-rw-r--r-- 1 root root 0 Apr  2 22:52 --2

-rw-r--r-- 1 root root 0 Apr  2 22:52 \-2

-rw-r--r-- 1 root root 0 Apr  2 22:52 -3

-rw-r--r-- 1 root root 0 Apr  2 22:52 --3

-rw-r--r-- 1 root root 0 Apr  2 22:52 \-3

-rw-r--r-- 1 root root 0 Apr  2 22:52 -4

-rw-r--r-- 1 root root 0 Apr  2 22:52 --4

-rw-r--r-- 1 root root 0 Apr  2 22:52 \-4

-rw-r--r-- 1 root root 0 Apr  2 22:52 -5

-rw-r--r-- 1 root root 0 Apr  2 22:52 --5

-rw-r--r-- 1 root root 0 Apr  2 22:52 \-5

我们试着用普通的rm删除时会提示:[root@huntdb aa]# rm -f --0

rm: unrecognized option `--0'

Try `rm --help' for more information.

查看rm man手册有如下描述:To remove a file whose name starts with a ‘-’, for example ‘-foo’, use one of these commands:

rm -- -foo

rm ./-foo

使用这个办法删除带\的文件进应注意进行二次转义,否则无法删除。[root@huntdb aa]# rm -f -- \-5

[root@huntdb aa]# ls

-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  --5  \-5

[root@huntdb aa]# rm -f ./\-5

[root@huntdb aa]# ls

-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  --5  \-5

正确的做法如下:[root@huntdb aa]# rm -f -- \\-5

[root@huntdb aa]# ls

-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  --5

[root@huntdb aa]# rm -f ./\\-4

[root@huntdb aa]# ls

-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  --5

文件夹的删除与此相同。

还可以通过inode号进行删除:[root@huntdb aa]# ls -li | grep -- -2

2752777 -rw-r--r-- 1 root root 0 Apr  2 22:52 -2

2752783 -rw-r--r-- 1 root root 0 Apr  2 22:52 --2

[root@huntdb aa]# find . -inum 2752783 -exec rm {} \;

[root@huntdb aa]# ls -li | grep -- -2

2752777 -rw-r--r-- 1 root root 0 Apr  2 22:52 -2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值