1830350 Members
1932 Online
110001 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
Michael Schulte zur Sur
Honored Contributor

Re: rm a file named -s

Hi Jeff,

wont your rm fail with files, that start with a dot? The safest method of course is the method to find out the inode number and then with find delete it. I wonder, if rm \-s works too.

have fun,

Michael
Jeff Schussele
Honored Contributor

Re: rm a file named -s

Hi Michael,

I guess I fat-fingered that & didn't notice it was supposed to be

rm * .*

So that .-leading files would be tagged as well.

The spaces are SO hard to see in the forum now.

Cheers,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Jeff Schussele
Honored Contributor

Re: rm a file named -s

Oops that was supposed to be....

rm -i * .*

SEE how hard that space is to see & padding it with more & they get dropped I think - let's test:

rm -i * .*

Later,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Jeff Schussele
Honored Contributor

Re: rm a file named -s

Yep - there WERE 4 spaces between first * and the . in that last post.

Later,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Bill Hassell
Honored Contributor

Re: rm a file named -s

A very important note about trying to remove files that start with . -- ALWAYS use the -i option with .* because this matches 2 or more characters which INCLUDES .. and if you happen to be in /home/bill, rm -r .* will remove *EVERY* file and directory in the /home directory! The reason is that a very important directory .. also matches and that is /home. Another method is to use rm .[!.]* which will not remove any file or directory that starts with ..


Bill Hassell, sysadmin
Kevin Wright
Honored Contributor

Re: rm a file named -s

from the rm man page..
It is forbidden to remove the file .., in order to avoid the consequences of using a command such as:
rm -r .*