Cannot delete special named files in terminal

Some program makes dummy files of the root directory, such as

-1 -2 -3 ... -n 

I run unsuccessfully

 rm -1 

and also

 rm "-1" 

The terminal considers the parameter -1 to be an option.

How to delete files in the terminal?

+1
source share
2 answers

You can use rm ./-1 / Refers to the current directory and, since the parameter does not start with a dash, it is not interpreted as an option.

+7
source

As far as I remember, adding - as an option on the command line, will force the rm command to take into account all other arguments literally, therefore the command

 rm -- -1 

Remove funny named files. Note that you can still use shellextensions (for example, "*" or "?"), Since the shell expands them before running the command (unlike DOS).

Edit: When I first encountered this problem, I did not know about this switch, so I wrote a small c program that will remove the file name in the same way as the first argument. This is easy to do, since all posix operating systems contain a unlink system call that deletes the file with the name specified as an argument (dump in the following 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 need to change gcc to cc or what you called the local C compiler.

+5
source

All Articles