UNIX: Remove a file with a name starting with – character

by Vivek Gite on November 30, 2007 · 6 comments

Q. Accidentally, I had created a file called -foo. Now how do I remove a file with a name starting with '-' under UNIX or Linux operating system?

A. Use standard UNIX rm command, as follows:

rm command

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

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 6 comments… read them below or add one }

1 Rich November 30, 2007

The following will work as well.

rm — \-foo

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

Reply

2 Royce December 12, 2007

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

Litter Delenda Est: part one

Litter Delenda Est: part two

Reply

3 Jadu January 2, 2008

$ rm — -file

this will also work.

Reply

4 Daevid Vincent April 17, 2009

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”

Reply

5 iqbal January 5, 2012

thanks man it help

Reply

6 irem February 10, 2012

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.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 6 + 12 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: