maven里如何根据不同的environment打包 为什么*nix下目录需要有可执行权限
Jan 08

前几天一个同事在服务器上务操作,生成了一个以"-"开始的文件.问我怎么删除.很容易想到的方法是 加引号 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

7 Responses to “*nix下如何处理带'-'的文件(夹) -xxx”

  1. Sutra Says:

    有这么累吗?写上绝对路径或者用其它目录或者其父目录参照一下,只要不是打头的就完事了吧:

    [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% 

  2. 莫多(somebody) Says:

    wow没有想到这招呢。昨天只想到可以把上级目录删除了。还是这个简单。

  3. ptubuntu Says:

    有时一紧张就会给忘记了.静下来了.什么问题都能解决的.

  4. 123123 Says:

    按时打算打算的

  5. darkraven Says:

    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
    ....

  6. fred Says:

    我i直接 rm -rf / 把<a href="http://www.skapal.com/">我的网站</a>搞挂了。

  7. polo shirts for men Says:

    You said very detailed ! Thank you for sharing.Your article written in very meaning, looking forward to you better masterpiece!

Leave a Reply

Identifying Code