Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

unix

rm

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?

like image 396
jrjc Avatar asked Feb 26 '14 17:02

jrjc


People also ask

How do I remove a file with the name '- something?

How do I remove or access a file with the name '-something' or containing another strange character ? If your file starts with a minus, use the -- flag to rm; if your file is named -g, then your rm command would look like rm -- -g.

Why there is * At the end of file name?

It means that the file is executable.

Which command is used for removing file named?

Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory.

How do I delete a file starting with Linux?

You can use standard UNIX or Linux rm command to delete a file name starting with - or -- . All you have to do is instruct the rm command not to follow end of command line flags by passing double dash -- option before -foo file name.


1 Answers

Thanks to @ajp15243 : the answer is :

rm ./-myfile.txt

or

rm -- -myfile.txt
like image 99
jrjc Avatar answered Oct 05 '22 07:10

jrjc