How to remove a file whose name starts with '-' [duplicate]

和自甴很熟 提交于 2019-12-30 04:50:08

问题


After a mistake in a script I ended up with a file whose name starts with a dash, -:

-myfile.txt

I tried so far:

rm -myfile.txt
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

rm "-myfile.txt"
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

rm "\-myfile.txt"
rm: \-myfile.txt: No such file or directory

rm \-myfile.txt
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

rm "-"myfile.txt
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

How can I delete this file?


回答1:


Thanks to @ajp15243 : the answer is :

rm ./-myfile.txt

or

rm -- -myfile.txt



回答2:


Use the absolute pathname

Like rm /home/name/-myfile.txt




回答3:


you could always try the inode solution :

$ ls -al -i | grep me
2116530 -rw-rw-r--  1 user123 user123       0 Feb 27 12:39 me

and 2116530 is the inode of the file. Then you can use find to delete it

find ./ -inum 2116530 --delete

or even

find ./ -inum 2116530 -exec rm {\} \;



回答4:


try this one put your file name in "-" eg: rm "-myfile.txt"



来源:https://stackoverflow.com/questions/22048216/how-to-remove-a-file-whose-name-starts-with

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