1753292 Members
6483 Online
108792 Solutions
New Discussion юеВ

rm a file named -s

 
SOLVED
Go to solution
Scott Lindstrom_2
Regular Advisor

rm a file named -s

I ended up with a file named -s. We have tried all sorts of options to try to delete this file (safely) with no success.

Everything we try sees the filename as a switch.

(I have been doing an 'll' on whatever pattern we try before we'll change the ll to a rm).

Any ideas?

Scott
15 REPLIES 15
Pete Randall
Outstanding Contributor
Solution

Re: rm a file named -s

rm -- -s


Pete

Pete
Scott Lindstrom_2
Regular Advisor

Re: rm a file named -s

Pete -

That did it.

Can you explain to me why it works?

Scott
Pete Randall
Outstanding Contributor

Re: rm a file named -s

Scott,

In all honesty, I can't really explain it. Somehow the -- flag removes the significance of the - flag, which is normally interpreted as an option. Somewhere in the depths of the man pages for the shell it's probably explained but I've never been inspired to search it out.


Pete

Pete
Jeff Schussele
Honored Contributor

Re: rm a file named -s

Hi Scott,

Another easy way to remove any weirdly named file is to go to that dir & do

rm -i *.*

And answer NO to all files displayed EXCEPT the one you want gone. This will even give you a chance to delete files with nothing but undisplayable chars in their name.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Kent Ostby
Honored Contributor

Re: rm a file named -s

I will generally use the following:

ll -i

in the directory to get the inode value (it will be in the left hand column)

THEN:

find . -inum -exec rm {} \;

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Ralph Haefner
Frequent Advisor

Re: rm a file named -s

The way I've heard the -- explained is that -- means "end of options list" and that anything else is not treated as an option.

In this case, it would mean tell rm that what follows is not an option, it is the filename.

Pete Randall
Outstanding Contributor

Re: rm a file named -s

Ok, my curiosity finally convinced me to look at the man page.

From man sh-posix:

-- Do not change any of the options; useful in setting parameter 1 to a value beginning with -.

That's what we're doing, setting parameter 1 (the -s) to a value beginning with -.


Pete

Pete
Todd McDaniel_1
Honored Contributor

Re: rm a file named -s

I cant believe that this hasn't been stated...


rm ./-s

You can also make the file with ./

touch ./-s # will create the file....
Unix, the other white meat.
Bill Hassell
Honored Contributor

Re: rm a file named -s

Since ll (or ls) may have any number of options supplied like: ll -t -i -b (those are valid options) then there is no way to tell which is an option and which is a filename. As mentioned, two dashes -- means no more option parsing, so everything else is a filename. However, this double-dash option is not used in every HP-UX command, so the best way is to always use dot-slash ./ since this means: in the current directory, something that seasoned sysadmins will always use because having . or the current working directory in your PATH is a big security error.


Bill Hassell, sysadmin