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?
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.
It means that the file is executable.
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.
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.
Thanks to @ajp15243 : the answer is :
rm ./-myfile.txt
or
rm -- -myfile.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With