How to remove a file beginning with a dash (in Unix like mode) [duplicate]

吃可爱长大的小学妹 提交于 2019-12-13 10:19:58

问题


I have accidentally created a file in GitBash (a Unix like environment) with the name - -l (I have absolutely no idea how I managed to do this in the first place :)

Johnny (master #) scipy-tentative-numpy-tutorials $ ls -l
total 1
-rw-r--r--    1 Johnny   Administ      956 May  7 16:24 - -l
-rw-r--r--    1 Johnny   Administ      562 May  7 16:21 README.md

I wish to delete (remove) that - -l file.

I have tried a few ways but no luck. e.g.

rm "- -l"
rm "-\ \-l"
rm -\ \-l

These didn't work.

Please how do I delete the - -l file?

Thank you!


回答1:


Use a double hyphen (--) to stop flag parsing:

rm -- -\ -l

Alternatively, you can even use tab completion:

rm -- \- # Then press TAB immediately after typing the final -

This assumes that's the only file with a leading hyphen in the directory. Bash is smart enough to automatically escape the space in the middle of the file name.




回答2:


Try this:

rm ./-\ -l

or this:

rm -- -\ -l

The first solution tells the program that you're looking for a file in the current directory, thus implying the argument is not an option.

The second solution uses -- to tell the program that subsequent arguments starting with - are not options.



来源:https://stackoverflow.com/questions/30105732/how-to-remove-a-file-beginning-with-a-dash-in-unix-like-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!