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.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Time1m

ADVERTISEMENTS

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 ----

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"

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdig host ip nmap
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w

ADVERTISEMENTS
7 comments… add one
  • Rich Nov 30, 2007 @ 19:03

    The following will work as well.

    rm — \-foo

    Any unix utility that uses getopt will see the — as an end of options.

  • Royce Dec 12, 2007 @ 22:23

    [self-promotion warning, but I do think that it’s topical …]

    I wrote an article for Sys Admin a while back about deleting files with unusual characters in their filenames on many Unix-likes.

  • Jadu Jan 2, 2008 @ 13:03

    $ rm — -file

    this will also work.

  • Daevid Vincent Apr 17, 2009 @ 18:14

    I had a directory named “–help” on accident.

    # rm — “–help”
    rm: cannot remove `–help’: Is a directory

    so you have to do it this way:

    # rm -rf — “–help”

  • iqbal Jan 5, 2012 @ 14:08

    thanks man it help

  • irem Feb 10, 2012 @ 4:16

    my 4050n nerowtk printer doesn’t work with large images and certain pdf files in linux.. windows 7 it works flawlessly. I was impressed with Ubuntu’s printing , but it isn’t up to windows spec. It’s also a lot slower with my nerowtk printer.

  • shailesh Dec 15, 2014 @ 13:10

    i typed dash on linux terminal and it changed. now their is only $ sign but not its full [user_name@user]$
    please tell me how to disable this dash

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.