前几天一个同事在服务器上务操作,生成了一个以"-"开始的文件.问我怎么删除.很容易想到的方法是 加引号 rm "-xxx",不行.再来加个转意符 rm \-xxx 也不行.
rory@gentoo ~/aaa $ ll
total 0
-rw-r--r-- 1 rory rory 0 2009-01-08 21:59 -xxx
rory@gentoo ~/aaa $ rm "-xxx"
rm: invalid option -- x
Try `rm ./-xxx' to remove the file `-xxx'.
Try `rm --help' for more information.
rory@gentoo ~/aaa $ rm "\-xxx"
rm: cannot remove `\\-xxx': No such file or directory
rory@gentoo ~/aaa $ rm \-xxx
rm: invalid option -- x
Try `rm ./-xxx' to remove the file `-xxx'.
Try `rm --help' for more information.
然后还跑到gentoo-cn的irc里去问了一下.有位兄弟告诉了方法.现在忘了是谁了. :(
原来是在前面添加 --
rory@gentoo ~/aaa $ rm -- -xxx
rory@gentoo ~/aaa $ ll
total 0
后来man 了一下rm,原来里面有说的.其实 rm --help里也是有的.
rory@gentoo ~/aaa $ 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
By default, rm does not remove directories. Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a `-', for example `-foo',
use one of these commands:
rm -- -foo
rm ./-foo
Note that if you use rm to remove a file, it is usually possible to recover
the contents of that file. If you want more assurance that the contents are
truly unrecoverable, consider using shred.
Report bugs to <bug-coreutils@gnu.org>.
都是自己不仔细.没有想到先man一下.不但可以rm,也可以建立哦.
rory@gentoo ~/aaa $ mkdir -- -xxx
rory@gentoo ~/aaa $ ll
total 4
drwxr-xr-x 2 rory rory 4096 2009-01-08 22:05 -xxx
rory@gentoo ~/aaa $ touch -- -yyy
rory@gentoo ~/aaa $ ll
total 4
drwxr-xr-x 2 rory rory 4096 2009-01-08 22:05 -xxx
-rw-r--r-- 1 rory rory 0 2009-01-08 22:05 -yyy
Like others

Friday, January 09, 2009, 12:52:16 AM (CST)
有这么累吗?写上绝对路径或者用其它目录或者其父目录参照一下,只要不是打头的就完事了吧:
[sutrademacbook-air:~/tmp/test] sutra% mkdir ./-test
[sutrademacbook-air:~/tmp/test] sutra% ll
total 0
drwxr-xr-x 2 sutra staff 68 1 9 00:50 -test
[sutrademacbook-air:~/tmp/test] sutra% rm -rf ./-test
[sutrademacbook-air:~/tmp/test] sutra% ll
[sutrademacbook-air:~/tmp/test] sutra%
Friday, January 09, 2009, 09:50:00 AM (CST)
wow没有想到这招呢。昨天只想到可以把上级目录删除了。还是这个简单。
Thursday, January 22, 2009, 05:18:58 PM (CST)
有时一紧张就会给忘记了.静下来了.什么问题都能解决的.
Tuesday, February 24, 2009, 01:16:35 PM (CST)
按时打算打算的
Saturday, April 24, 2010, 02:56:51 PM (CST)
rory@gentoo ~/aaa $ rm "-xxx"
rm: invalid option -- x
===Try `rm ./-xxx' to remove the file `-xxx'.====
Try `rm --help' for more information.
不仔细看说明....
rm ./-xxx
....
Monday, July 11, 2011, 08:49:37 PM (CST)
我i直接 rm -rf / 把<a href="http://www.skapal.com/">我的网站</a>搞挂了。
Thursday, July 19, 2012, 09:57:41 AM (CST)
You said very detailed ! Thank you for sharing.Your article written in very meaning, looking forward to you better masterpiece!