UNIX: Remove A File With A Name Starting With – (dash) Character

I am a new Unix shell user at my university shell server. Accidentally, I had created a file called -foo. Now, how do I remove a file with a name starting with ‘-‘ under UNIX-like or Linux operating system?

You can use standard UNIX/Linux rm command. 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.

Many user creates these kind of file accidentally with dashes. If you attempt to remove such file via rm command, UNIX and Linux will attempt to use them as command-line options and the command will display out with an error. So how do you get rid of these files and delete them on a UNIX?

Fig.01: Remove the file with —- Dashes —-

rm command syntax

Use rm command to remove files or directories as follows:
rm -- -foo
OR
rm ./-foo
OR
rm ./-filename

Other options: Unix remove file with dash

Some more options remove the —- Dashes —- on a Unix-like system:

rm ./-Foo
rm "./---bar"
rm -- -F
rm -- ---footbal
## Edit file ##
vi  "./--foo"

rm ./-Foo
rm "./—bar"
rm — -F
rm — —footbal
## Edit file ##
vi "./–foo"

Alternatively, you can also use UNIX / Linux inode number to delete a file with strange names.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.