Unable to remove a special named files in terminal

扶醉桌前 提交于 2019-12-20 04:16:04

问题


Some program makes ta my root directory dummy files such as

-1
-2
-3
...
-n

I run unsuccessfully

rm -1

and too

rm "-1"

Terminal thinks that -1 is the option.

How can you remove the files in terminal?


回答1:


You can use rm ./-1, the ./ refers to the current directory and as the parameter doesn't start with a dash it isn't interpreted as an option.




回答2:


As far as I recall adding -- as an option on the commandline will cause the rm command to consider all the remaining arguments literally, so the command

rm -- -1

Will remove the funny named files. Note that you can still use shellextensions (such as '*' or '?') since the shell expand these before the command is run (unlike DOS).

Edit: When I was first faced with this problem, I didn't know about the -- switch so I wrote a small c program that would remove the file name the same as the first argument. This is easy to do since all posix operating systems contain the unlink system call which removes the file with the name given as argument (dump the following into a terminal):

remove_arg.c << EOF
#include<unistd.h>
int main(int argc, char **argv){
  unlink(argv[1]);
}
EOF
gcc -o remove_arg remove_arg.c
./remove_arg -1

This should work on any unix system, although you may have to change gcc into cc or what you local c compiler is named.



来源:https://stackoverflow.com/questions/791155/unable-to-remove-a-special-named-files-in-terminal

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