问题:
有个一文件名里面还有特殊的字符,如"-osample1.c", 但是很难删除这个文件,因为如果删除的时候,系统会认为这个文件名是你要使用的一个命令行选项从而无法删除。而且会报出“ls: illegal option -- .”这类的错误,情况如下
> ls *sample1*
ls: illegal option -- .
usage: ls -1ARadeCxmnlogrtucpFLbqisf [files]
> ls
-osample1.o demo_proc.mk makefile test.sh test1.c test1.o testing
asdf.log inst scripts test1 test1.lis test1.pc
> rm *sample1*
rm: illegal option -- o
rm: illegal option -- s
rm: illegal option -- a
rm: illegal option -- m
rm: illegal option -- p
rm: illegal option -- l
rm: illegal option -- e
rm: illegal option -- 1
rm: illegal option -- .
rm: illegal option -- o
Usage: rm [-Rfir] file ...
> rm -osample1.o
rm: illegal option -- o
rm: illegal option -- s
rm: illegal option -- a
rm: illegal option -- m
rm: illegal option -- p
rm: illegal option -- l
rm: illegal option -- e
rm: illegal option -- 1
rm: illegal option -- .
rm: illegal option -- o
Usage: rm [-Rfir] file ...
解答:
查看系统的帮助手册 man sh
Code:
-- A -- signals the end of options and disables further option
processing. Any arguments after the -- are treated as file-
names and arguments. An argument of - is equivalent to --.
根据描述,我们可以进行如下的操作
Code:
sh-2.05b$ touch -- -123
sh-2.05b$ ls -l -- -123
-rw-r--r-- 1 abcdefgh g900 0 Dec 7 07:17 -123
sh-2.05b$ rm -- -123
sh-2.05b$ ls -l -- -123
ls: -123: No such file or directory
sh-2.05b$