How do I deal with a filename that starts with the hyphen (-) character?

后端 未结 4 1181
感情败类
感情败类 2020-11-28 08:34

Somehow, at some point, I accidentally created a file in my home directory named \'-s\'. It is about 500 kb and I have no idea if it contains important data or not. I cannot

相关标签:
4条回答
  • 2020-11-28 08:55

    You can get rid of it with:

    rm ./-s
    

    The rm command (at least under Ubuntu 10.04) even tells you such:

    pax@pax-desktop:~$ rm -w
    rm: invalid option -- 'w'
    Try `rm ./-w' to remove the file `-w'.
    Try `rm --help' for more information.
    

    The reason that works is because rm doesn't think it's an option (since it doesn't start with -) but it's still referring to the specific file in the current directory.

    0 讨论(0)
  • 2020-11-28 08:57

    You can refer to it either using ./-filename or some command will allow you to put it after double dash:

    rm -- -filename
    
    0 讨论(0)
  • 2020-11-28 09:05

    You could use --, e.g.:

    rm -- -file
    
    0 讨论(0)
  • 2020-11-28 09:18

    besides using rm, if you know a language, you can also use them. They are not affected by such shell warts.

    Ruby(1.9+)

    $ ruby -rfileutils -e 'FileUtils.rm("-s")'
    

    or

    $ ruby -e 'File.unlink("-s")'
    
    0 讨论(0)
提交回复
热议问题