Unable to remove a special named files in terminal

2019-03-02 04:36发布

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?

标签: rm
2条回答
▲ chillily
2楼-- · 2019-03-02 05:06

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.

查看更多
老娘就宠你
3楼-- · 2019-03-02 05:22

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.

查看更多
登录 后发表回答